• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

PHP Code Problem

netsider

Limp Gawd
Joined
Oct 12, 2004
Messages
466
I posted both pieces of code here:
http://pastebin.com/nPa4x5tP

It should be pretty obvious what I'm trying to do. I found this example online of a form that was able to edit a text file, but it doesn't work (I absolutely *hate* when people submit examples that do not work, and yes, hate is a strong word). Sometimes I realize I may not be doing something right, and that's why it doesn't work.. but I'm stumped on this one, and I seem to be using fwrite() correctly, although I can't find a specific example online using $_POST.

At first it was giving me an error saying fwrite expected a string, but a resource was given. After playing around with it, now it doesn't return any errors. What am I doing wrong??

I've created the file and it's in the same directory as these files (data.txt).

It appears to work, but then doesn't replace what is currently in the file with the $_POST data being submitted. I'm sure it's something simple.. but it's driving me nuts and I cannot figure it out.

I figured maybe it was replacing what's in the file, with what's already in the file.. but the post data should contain whatever is being submitted in the form.

Can someone please correct this for me, or tell me how?

Thanks :confused:

Edit: Here's the original place I got the example. I just looked at it again, and can't find anything in it that I changed to make it not work, or anything like that. It just didn't seem to right off-the-bat, but maybe you guys will have a little better luck:
http://phpsnips.com/21/Edit-And-Save-A-File#.U65zm7GU93l
 
Last edited:
Try using " instead of ' in fopen mode. Like this:
Code:
 fopen($_POST['filename'],"w");

Sorry if it doesn't help but I had problems like this before in PHP and it is one of the reasons why I don't like it...
 
I posted both pieces of code here:
http://pastebin.com/nPa4x5tP

It should be pretty obvious what I'm trying to do. I found this example online of a form that was able to edit a text file, but it doesn't work (I absolutely *hate* when people submit examples that do not work, and yes, hate is a strong word). Sometimes I realize I may not be doing something right, and that's why it doesn't work.. but I'm stumped on this one, and I seem to be using fwrite() correctly, although I can't find a specific example online using $_POST.

At first it was giving me an error saying fwrite expected a string, but a resource was given. After playing around with it, now it doesn't return any errors. What am I doing wrong??

I've created the file and it's in the same directory as these files (data.txt).

It appears to work, but then doesn't replace what is currently in the file with the $_POST data being submitted. I'm sure it's something simple.. but it's driving me nuts and I cannot figure it out.

I figured maybe it was replacing what's in the file, with what's already in the file.. but the post data should contain whatever is being submitted in the form.

Can someone please correct this for me, or tell me how?

Thanks :confused:

Edit: Here's the original place I got the example. I just looked at it again, and can't find anything in it that I changed to make it not work, or anything like that. It just didn't seem to right off-the-bat, but maybe you guys will have a little better luck:
http://phpsnips.com/21/Edit-And-Save-A-File#.U65zm7GU93l

What does the command line output of this look like? What's in the error file?

Also use http://www.php.net/manual/en/function.file-get-contents.php instead of bothering with that loop. Just to save some code space. Pair it with http://www.php.net/manual/en/function.file-put-contents.php in the other file. Will make your life simpler, and built in PHP functions are generally faster than doing it step by step on your own.

This is something that might trip you up:
Make sure that Apache or whatever server you're using has permissions to write to the file. What operating system are you using? If it's some Linux variant remember that you are NOT the user that is actually trying to write to the file. wwwdata (or something like that) is the one running the file, or whatever user proxy Apache (or your web server) is using. It might not have permissions to the file if you (the user) made it.

Finally, I don't think you want to let users write arbitrary information to a text file. Suppose that they started writing some kind of code to it and found a way to execute it. I don't think you want that. It would be better to store these "documents" as a text field in a database and then use prepared queries to update them.
 
Finally, I don't think you want to let users write arbitrary information to a text file. Suppose that they started writing some kind of code to it and found a way to execute it. I don't think you want that. It would be better to store these "documents" as a text field in a database and then use prepared queries to update them.
Furthermore, on this point; I don't think OP wants to allow users to try to write to arbitrary files on the system.

Maybe the code here is mockup, but if not: OP, you will want to sanitize not only your txt data coming in from the form, but also the file name. At the very least, strip it of any pathing information and hardcode the path in your code.

As to the actual error, what do the web server's error logs say?
 
Furthermore, on this point; I don't think OP wants to allow users to try to write to arbitrary files on the system.

Maybe the code here is mockup, but if not: OP, you will want to sanitize not only your txt data coming in from the form, but also the file name. At the very least, strip it of any pathing information and hardcode the path in your code.

As to the actual error, what do the web server's error logs say?

Yeah, this was just a simple offline example to see how fwrite, fopen, etc work.
 
Try using " instead of ' in fopen mode. Like this:
Code:
 fopen($_POST['filename'],"w");

Sorry if it doesn't help but I had problems like this before in PHP and it is one of the reasons why I don't like it...

a million ways to do the same thing and they all sometimes work? :)
 
How does it appear to work?

Is your display_errors directive set to 1 in php.ini?

Why not just use file_put_contents()?
 
Back
Top