Page 1 of 1

fopen problem

Posted: Fri Jun 16, 2006 11:55 am
by techleet
Hi Guys,

I'm stuck here...
I'm opening a file, messing with the contents, and writing it to another file.

- Both files open fine.
- Destination file opens fine, creates a blank file (like it should)
- is_writable($destinationFile) comes up false! Wtf?

Here's my code:

Code: Select all

$inpath = $_SERVER['DOCUMENT_ROOT']. "\\web\\";
$outpath = $_SERVER['DOCUMENT_ROOT']. "\\web\\output\\";

$handle = fopen($inpath . $fileNames[$i], "r");
$newFile = fopen($outpath . $fileNames[$i], 'w+');

while (!feof($handle)) {
   $theLine = fgets($handle);
   // Do stuff to $theLine

}

if(is_writable($newFile)) {
   echo "WRITABLE:  ";
   if(fwrite($newFile,$LineDone) === false) echo "<br>Error WRITING TO FILE!!"; 
   else echo "<br>Written";
}
else {  echo "UNWRITABLE"; }
I always get "UNWRITABLE"... what gives? I opened the file with 'w+'....? I've also tried 'w', 'wb', 'r+'.

:?:

Thanks!

Posted: Fri Jun 16, 2006 6:32 pm
by feyd
close the file first.

Posted: Fri Jun 16, 2006 6:45 pm
by techleet
feyd wrote:close the file first.
Close the file before writing to it....?

Posted: Fri Jun 16, 2006 6:49 pm
by techleet
I figured it out.

Apparently, fwrite requires that the target file be in the same working directory.

This would never work:

Code: Select all

$inpath = $_SERVER['DOCUMENT_ROOT']. "\\web\\"; 
$outpath = $_SERVER['DOCUMENT_ROOT']. "\\web\\output\\"; 

$handle = fopen($inpath . $fileNames[$i], "r"); 
$newFile = fopen($outpath . $fileNames[$i], 'w+');
I had to change it to this:

Code: Select all

$handle = fopen($fileNames[$i], "r"); 
$newFile = fopen($fileNames[$i] ."_fixed", 'w+');
...and I had to have this code, as well as all files in the same directory.