Jump to content

Read/Write permissions


zimmy52420

Recommended Posts

I have created, what I have been assured is a perfectly valid script... I have asked several people who seem to know what they are talking about in the field, and they assure me there is nothing wrong with this. However I recieve the following errors whenever I use the scriptWarning: fopen(./chatlog.html): failed to open stream: Success in /home/fhlinux183/c/canine-haven.co.uk/user/htdocs/chat/action.php on line 10Warning: fwrite(): supplied argument is not a valid stream resource in /home/fhlinux183/c/canine-haven.co.uk/user/htdocs/chat/action.php on line 11Warning: fclose(): supplied argument is not a valid stream resource in /home/fhlinux183/c/canine-haven.co.uk/user/htdocs/chat/action.php on line 12I am using Dead Rat Linux on my server, and following a different PHP support group's suggestions, went through the shell, and did CHMOD 777 on the file, and it still is not working. The code is below.

<?php$name = $_REQUEST["name"];$message = $_REQUEST["message"];$filename = "./chatlog.html";fopen($filename, "br+");fwrite($filename, "$name<BR>$message<P>");fclose($filename);?>

I have tried various different combinations of b r+ and t (br+, r+b, tr+, r+t, r+) some of these removed the first error, but kept the others, some changed 'Success in /home/fhlin...' to 'Failure in /home/fhlin...' but otherwise, yielded no resultsThe server can READ chatlog.html, as in a different file it is read with the include function,Well... I hope I've been detailed enough, if there's anything important I've left out, please tell me, as I'm REALLY after getting this fixed, if it is any help at all, the script can be located at http://www.canine-haven.co.uk/chatMany thanks in advance

Link to comment
Share on other sites

This is from php.net:

you can also use 'b' to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.
What messages do you get when the mode is r+b?Also, since all you are doing is writing, you might as well use either wb or ab for the mode instead of r+.
Link to comment
Share on other sites

It removes the first error, but keeps the other 2, and doesn't write to the fileI'll try the two other combinations you gave me now,EDIT:The other two give the same two errors, and do they leave the previous contents of the page, and place the pointer at the front of the file? :/Heh, I think my live chat script just doesn't like me.Another EDIT:One of the combinations you gave me truncated the file length, which I find odd. Any ideas why it can delete but not write? o_O

Link to comment
Share on other sites

The 'a' mode is for append, and will keep the contents of the file and place the pointer at the end. The 'w' mode does truncate the file to zero length, I didn't realize that. You can get an overview of the modes here:http://www.php.net/manual/en/function.fopen.phpFor fwrite and fclose, you do not pass the filename. The return value of fopen is a file resource which you need to pass to the next two functions. So, the experts you talked to got that one wrong.$fp = fopen(...);fwrite($fp, "...");fclose($fp);

Link to comment
Share on other sites

I've probably got this wrong somewhere along the line, but if I understood correctly you mean

$name = $_REQUEST["name"];$message = $_REQUEST["message"];$fstuff = "fopen(./chatlog.html, r+b)";fwrite($fstuff, "$name<BR>$message<P>");fclose($fstuff);

I've tried it, and I'm still getting the same errors, any suggestions, or have I just managed to misunderstand what you've told me?

Link to comment
Share on other sites

You have the entire call to fopen in quotes. Remove the quotes, fopen returns a resource that you need to store in a variable and send to fwrite and fclose, and all other file manipulation functions. Check the php.net reference pages for fopen or fwrite to see plenty of examples.

Link to comment
Share on other sites

... why not use file_get_contents() and file_put_contents() instead of fopen, fwrite, and fclose? just go to php.net and search in the functions list to find out how to use those.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...