Problems opening a text file

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
SteveD
Forum Newbie
Posts: 3
Joined: Sat Jun 10, 2006 11:16 pm

Problems opening a text file

Post by SteveD »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I've done this several times, using the same code (I think).  But for some reason I'm getting a strange error when I try to open a text file.

Code: Select all

$file = "lunch.txt";                        // define the filename for output file
print "Set Filename: $file<br>\n";  // debug line
  $fhandle = @fopen($file,'a+');
print "File handle is: $fhandle<br>\n";  // debug line
  /* if the file is non-zero in length, add a newline before entering this data
      to separate it from the other data already there.
      If the length=0 then do not start the file off with a newline. */
  if (@filesize($file)!==0)   {  
    fwrite ($fhandle,"\n"); 
    }// if

  fwrite ($fhandle,"$week=$month=Monday=$day=$breakfast_mon=$lunch_mon\n");
etc.

Error msg in my browser says:
Warning: fwrite(): supplied argument is not a valid stream resource in C:\Inetpub\wwwroot\lunch\LunchOutput.php on line 97

I get this error message for each fwrite and for the fclose. It appears it is going to the C: drive instead of the server, which I don't get. I'm sure this code worked a week ago. Another strange thing is that the line number in the error message is exactly one line off for each. I don't recall seeing that before, is that a clue?

Thanks for any advice, I'm going crazy here.
SteveD


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
shneoh
Forum Commoner
Posts: 38
Joined: Mon Sep 25, 2006 2:23 am
Location: Malaysia

Post by shneoh »

Try to remove the "@" sign and run again. Post the error msg.
SteveD
Forum Newbie
Posts: 3
Joined: Sat Jun 10, 2006 11:16 pm

Post by SteveD »

With the @ removed I get the following output:

Set Filename: lunch.txt

Warning: fopen(lunch.txt) [function.fopen]: failed to open stream: Permission denied in C:\Inetpub\wwwroot\lunch\LunchOutput.php on line 91
File handle is:

Warning: fwrite(): supplied argument is not a valid stream resource in C:\Inetpub\wwwroot\lunch\LunchOutput.php on line 97


You can see the first debug line displays the filename correctly, the second debug line is empty - it didn't take the filehandle.
shneoh
Forum Commoner
Posts: 38
Joined: Mon Sep 25, 2006 2:23 am
Location: Malaysia

Post by shneoh »

It look like a write permission problem. Check if this file is writable.

BTW, have you tried to use a full path for the open file?
Post Reply