Re: Problem writing to a file
| From: | Stephen | Date: | Wed, 10 Sep 2003 11:23:30 +0000 |
| Subject: | Re: Problem writing to a file | ||
| References: | 1 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-21338@lists.php.net to get a copy of this message | ||
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