fopen problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
techleet
Forum Newbie
Posts: 10
Joined: Wed May 17, 2006 10:51 am
Location: San Jose, CA

fopen problem

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

close the file first.
User avatar
techleet
Forum Newbie
Posts: 10
Joined: Wed May 17, 2006 10:51 am
Location: San Jose, CA

Post by techleet »

feyd wrote:close the file first.
Close the file before writing to it....?
User avatar
techleet
Forum Newbie
Posts: 10
Joined: Wed May 17, 2006 10:51 am
Location: San Jose, CA

Post 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.
Post Reply