ThePsion5 Posted July 18, 2007 Share Posted July 18, 2007 Hi guys. I'm writing a PHP script that involves creating and then accessing a file. I'm running into a strange problem, however: when I use fopen() to open the file, it succeeds (does not generate any warnings/errors) but instead of returning a file handle, it returns 1/true. Any idea what might be causing this? if(!file_exists($Filename)) //Create the file handle $FileHandle = fopen($Filename, 'w'); //If the file could not be opened or created, throw an exception print_r($FileHandle); //PROBLEM: Prints 1 instead of the file handle if(!$FileHandle) throw new Exception('Unable to create or open the following file for writing: "'.$Filename. '" in directory "'.$this->CacheFileLocation.'"'); else { //Save the data in the file fwrite($FileHandle, $Content); //Here i get an error saying that "$FileHandle" is not a valid resource //Close the file stream handle fclose($FileHandle); //Same here } Link to comment Share on other sites More sharing options...
justsomeguy Posted July 18, 2007 Share Posted July 18, 2007 Instead of using print_r, use var_dump, it will also give the type of the variable. Link to comment Share on other sites More sharing options...
ThePsion5 Posted July 18, 2007 Author Share Posted July 18, 2007 I did, and it's returning a boolean (true) value. Very strange... Link to comment Share on other sites More sharing options...
justsomeguy Posted July 18, 2007 Share Posted July 18, 2007 Yeah, that's weird allright. The documentation for fopen never says that it will return true under any circumstances. Just as a sanity check, this prints a boolean?var_dump(fopen($Filename, 'w')); Link to comment Share on other sites More sharing options...
jhecht Posted July 19, 2007 Share Posted July 19, 2007 uhm, my personal preferences say use file_get_contents and file_put_contents. Just as a thought on this one. See if those return the same results. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 19, 2007 Share Posted July 19, 2007 file_put_contents is only available in PHP5. Also, the documentation for file_put_contents says that is it identical to calling fopen, fwrite, and fclose. This should work, there's no reason why fopen should return boolean true, regardless of what file_put_contents does. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.