Help - Search - Members - Calendar
Full Version: keeping for record after form correction
HTMLHelp Forums > Programming > Server-side Scripting
asmith
Hey guys

This has almost gave me a big headache.

I have a form,
when a user fills some part of it wrong, like enter bad type for email address the form loads again and tell him to fix his email, then other fields will remain the thing the user had typed, for example :


CODE
<input type="text" name="username" value="$_POST[username]" />


the code above will keep the username in the field.

Now the problem is,
I have an fileuploading field (for image). when the form is iewed again for user to correct his mistakes, I don't know how to keep his previously uploaded file :/

for example :

<input type="file" name="image" value=? there's no such value for that.

How can I keep this file temporary so that the user don't have to upload again his file ?
I mean temporary because maybe he dont' submit his form after he got errors, so if i save the file on the server, and he changes his mind, there would be a useless file on my server and I don't wanna use cron job to create an script to search for uselss files and delete them.

Is it possible I store a file in a PHP session???
What other people do in these cases?
QuickieHost
When your user uploads a file, it will get stored in the "/tmp" directory (or whatever temporary directory is configured on your server.)

PHP stores the temporary filename of the uploaded file in a variable as follows:

CODE
$_FILES['formfieldname']['tmp_name']


Where formfieldname is the name of the field for the file upload form field on your HTML page.

You could store this temporary filename into a session variable if you wanted to:

CODE
$_SESSION['uploaded_file'] = $_FILES['formfieldname']['tmp_name'];


Check out this PHP manual page for more info:
http://us.php.net/features.file-upload

Good Luck!

Jon
Brian Chandler
Or if you aren't using sessions, you could simply pass the name of the file back in the form as a hidden variable. (You need some checking then, to make sure no-one is trying to read someone else's file (etc etc), and you also (presumably?) need housekeeping to delete the file after some appropriate time (but perhaps php does that automatically?))
asmith
All true about the file name or tmp_name. But php delete the temp file automatically after the form is executed.
I mean if I save the tmp_name into a session, and the temp file get deleted, what good is that?
Or I've got the whole thing wrong ?


from php manual :
QUOTE
The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed.
Brian Chandler
QUOTE
All true about the file name or tmp_name. But php delete the temp file automatically after the form is executed. I mean if I save the tmp_name into a session, and the temp file get deleted, what good is that?


Well, obviously if the temp file gets deleted at the end of the program execution, you need to save it somewhere. What do you do if the form submission works first time? Just send everything in an email, with attached file? It's neater really to keep the info in a database, then adding an entry to remember submitted files is a trivial addition.

But even without that, just remember the name of the saved file (use a datestamp or similar for the filename), and pass it back in a hidden variable, then delete unused files, either manually or with a little script.
asmith
QUOTE
Just send everything in an email, with attached file? It's neater really to keep the info in a database


sure, I was saving the in information in database in the first place.
Well as I couldn't wait more, I did the only thing that came to my mind :

I save the file on the server no matter if the form has error or not. carry the saved file name in a session (or can be hidden input, I prefered session). then if the form is executed again, then save the file name in session to the database.

then I write a cron job task to check database and delete any file on the server than its name is not on the database.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.