Page 1 of 1
fwrite () question. PLEASE HELP!!
Posted: Sun Dec 07, 2003 2:25 am
by lazersam
Hi all
I am trying to write to a text file as follows;
Code: Select all
<?php
$filename = "test.txt";
$result=fopen($filename, "a+");
if (!$result){
echo "Did not open file"; exit ();
}
fwrite ($filename, "Test\n\n");
fclose($filename);
?>
It seems simple enough but i get this error....
Warning: fwrite(): supplied argument is not a valid stream resource in /home/lazersam/public_html/PHP/temp.php on line 16
Warning: fclose(): supplied argument is not a valid stream resource in /home/lazersam/public_html/PHP/temp.php on line 17
This is confusing because the fopen() statement works ok. I have set permissions for the directory but I am unable to set permission for test.txt which keeps defaulting back to 644.
Any ideas?
Thanks
Lawrence.
Posted: Sun Dec 07, 2003 2:57 am
by Pyrite
Does the file get created though?
Posted: Sun Dec 07, 2003 3:10 am
by lazersam
Pyrite wrote:Does the file get created though?
Yes. It creates the file.
I think it might have something to do with my permissions on the server. When I try to chmod the file I get an error 550.
Posted: Sun Dec 07, 2003 3:17 am
by Pyrite
You can add a if (is_writable($filename)) { code to your script to first see if the file can even be written to before you call fopen. That way you know it will be a permissions issue between probably the user that your webserver runs under.
Are you using Linux? Is it your server? Do you have root? What happens if you create the file yourself, and then try to let your script write to it?
Posted: Sun Dec 07, 2003 3:40 am
by lazersam
Pyrite wrote:You can add a if (is_writable($filename)) { code to your script to first see if the file can even be written to before you call fopen. That way you know it will be a permissions issue between probably the user that your webserver runs under.
Are you using Linux? Is it your server? Do you have root? What happens if you create the file yourself, and then try to let your script write to it?
I ran the is_writable() statement and it proved the file IS writable. The script does open the file.
When I create the file myself it allows me to change its chmod permissions (?) however - it still give the same error. Do you know why it doesn't allow me to change permissions if PHP creates the file?
Warning: fwrite(): supplied argument is not a valid stream resource in..........
Posted: Sun Dec 07, 2003 4:26 am
by Pyrite
Cause the webserver/php run as a different user than you, and files that they create, you have different permissions to. Like Apache probably runs under nobody, or httpd or some user like that. I'm guessing its not your box though, so I don't know what to tell you. Can you ask your host about it?
Posted: Sun Dec 07, 2003 4:31 am
by lazersam
Thanks for trying Pyrite,
I have contacted my host but I hold out little hope
It seems so unlikely. I have searched the web for hours trying to fix this problem. All I want to do is write to a text file. Its chmod is 777, the directory chmod is 777 even! I think my syntax is all ok!
Can anyone else help?
I have even tried on a different domain but get the same results.
Thanks
Lawrence.
Posted: Sun Dec 07, 2003 8:33 am
by lazersam
Its OK I solved it.....
its was a simple case of wrong syntax
HOW IT WAS:
Code: Select all
<?php
$filename = "test.txt";
$result=fopen($filename, "a+");
if (!$result){
echo "Did not open file"; exit ();
}
fwrite ($filename, "Test\n\n");
fclose($filename);
?>
HOW IT SHOULD HAVE BEEN:
Code: Select all
<?php
$filename = "test.txt";
$result=fopen($filename, "a+");
if (!$result){
echo "Did not open file"; exit ();
}
fwrite ($result, "Test\n\n");
fclose($result);
?>
Thanks
Lawrence

Posted: Sun Dec 07, 2003 8:50 am
by Nay
You mean wrong variable <_<. Heh, anyhow, here's an alternative:
Code: Select all
<?php
$file = "test.txt";
$fd = fopen($file, "a+") or die("Did not open file: " . $filename);
$fw = fwrite("test", $fd);
fclose($fd);
?>
-Nay
Posted: Sun Dec 07, 2003 10:38 am
by lazersam
Nay wrote:You mean wrong variable <_<. Heh, anyhow, here's an alternative:
Code: Select all
<?php
$file = "test.txt";
$fd = fopen($file, "a+") or die("Did not open file: " . $filename);
$fw = fwrite("test", $fd);
fclose($fd);
?>
-Nay
Cool - thanks

Posted: Sun Dec 07, 2003 11:34 am
by BDKR
Nay wrote:You mean wrong variable <_<. Heh, anyhow, here's an alternative:
Code: Select all
<?php
$file = "test.txt";
$fd = fopen($file, "a+") or die("Did not open file: " . $filename);
$fw = fwrite("test", $fd);
fclose($fd);
?>
-Nay
I think the "or die()" stuff is bad practice. We should want our scripts to terminate in a more elegant manner shouldn't we? Testing for the error then doing something like displaying a more understandable error message to the end user would make them a lot happier.
Cheers,
BDKR