Re: Problem writing to a file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Sorry to say it, but that is quite sloppy coding. This highlights why it is
important to give variables meaningful names! In the code below, George has
assigned the location of his file to a pointer, but then over-written it
with the pointer result of fopen.

> $fp2 = "C:\\inetpub\\wwwroot\\packtracker\\LSE\\".$cr.".txt";
> if (!$fp2 = fopen($fp2, 'w')) {
>     print "Cannot open the bloody file ($fp2)";
>     exit;
> }
> if (!fwrite($fp2, $content)) {
>     print "Cannot write to file ($fp2)";
>     exit;
> }
> fclose($fp2);
> rename($cr.".txt",$cr.".doc");

A better solution would be to have two different variables like this:

> $file_location = "C:\\inetpub\\wwwroot\\packtracker\\LSE\\".$cr.".txt";
> if ( !$file_pointer = fopen( $file_location, 'w' ) ) {
>     print "Cannot open the bloody file ( $file_location )";
>     exit;
> }
> if ( !fwrite( $file_pointer, $content ) ) {
>     print "Cannot write to file ( $file_location )";
>     exit;
> }
> fclose( $file_pointer );
> rename($cr.".txt",$cr.".doc");

As you can see, it makes it much more obvious what does what, and so easier
for you/others to come back to in a few months time to make changes to.
Don't be afraid to name your variables properly!

Stephen

-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux