Cannot read entire file into a variable

This is probably more of a pure ‘PHP’ question but what I am trying to do is clone a controller file and then just change the name of the file and the class. Similar to what gii does but without the template approach. I just want to clone an existing file and then change the class name.

I have tried a few ways to read in the file. This is the first few lines of the actual file that I want to read/clone.




<?php


class SBCmsController extends Controller


{

	/**

	 * Declares class-based actions.

	 */

    public function init()

    {

        $this->layout = '//layouts/column1';

        $this->defaultAction = 'home.html';


    }




	public function actions()

	{

Pretty basic stuff with the exception of an unusual default action.

This is the code that reads the file and echos the contents.


            $file_handle = fopen($controllerPath.DIRECTORY_SEPARATOR.$sourceController, "r");

            $num =0;

            while (!feof($file_handle)) {

             $line = fgets($file_handle);

                echo $num.$line;

                $num++;


                    }

            fclose($file_handle);

             die;

It seems like it should work and just for debugging I added the line # ($num), this is my output.

It is very strange that the beginning of the file is completely missing. It’s like PHP or Yii(?) is doing something because it is a class.

Any ideas?

I have tried a few other methods with the same result.




//            $sourceFile = Yii::app()->file->set($controllerPath.DIRECTORY_SEPARATOR.$sourceController);

//            $str=implode("\n",file($controllerPath.DIRECTORY_SEPARATOR.$sourceController));



I have tried ‘file’ and the ‘Cfile’ extension found in the extensions repository.

‘Cfile’ uses file_get_contents() internally.

Pretty stumped on this one. :blink:

doodle

Just an update, it is not a Yii problem it is PHP.

I used this code outside of Yii and got the same result.


<?php

echo '<pre>';

$file_handle = fopen('SBCmsController.php', "r");

            $num =0;

            while (!feof($file_handle)) {

             $line = fgets($file_handle);

                echo $num.$line;

                $num++;


                    }

            fclose($file_handle);

 

?>

this is weird

try using fread instead of fgets, something like




$filename=$controllerPath.DIRECTORY_SEPARATOR.$sourceController;

$handle = fopen($filename, "r");;

$contents = fread($handle, filesize($filename));

fclose($handle);



@Gustavo

Thanks, I’ll try that when I back to work on Monday

:sunglasses:

doodle

Update

Before trying Gustavo’s suggestion I uploaded the files to my production server. Locally I am running Vista (insert pounding head against wall here). The production server would not allow me to run the script through the browser, it returns a 501 Method not supported error.

Next I modified the script to match Gustavo’s suggestion but I go the same result. Next I ran the script from the command line on the production server, everything worked as expected, success! Next I ran the script on the Vista thing, it worked there too!

Since I was only trying to echo the file out for debugging purposes it may be that some server config prevents this and I am prepared to accept that this cannot be done.

doodle.

Update

Well I got everything working on my dev machine (vista). I expect that I will have to tweak the permissions on the production server (linux).

So it seems that it was a server/browser/permissions issue, I know just enough about servers to be dangerous so I’m not tweaking dev or production server.

Thanks for your help Gustavo! I ended up using the CFile extension, it has some built in error checking etc.

Does anyone know how to change the post topic to @@@@@ - solved.

doodle

There is no need to close or mark a topic as solved… becasue anybody can have the same problem in the future and continue this discussion…