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!
<?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.
Last edited by lazersam on Sun Dec 07, 2003 6:58 am, edited 1 time in total.
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?
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..........
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?
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.
<?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.