Caching FILE input field data

I have a form that gets submitted and then validated by a PHP class. The problem is that there are file input fields for uploading photos: “<input type=file” />"

If the form kicks back an error it goes back to the form with an error message. All of my other field values are passed back in session variables. For example, you entered your name, job title and company and also chose a photo to upload. You had an illegal character in your company name so it kicks you back to the form. Your name, job title and company are still filled in with $_SESSION[‘tmpName’] etc. for each field… these are unset as soon as they are used, to make sure they do not get re-used.

Anyhow, how in the heck to I pass the value of the file they selected back to the FILE box? Here’s how I set it for upload:

// Image upload path //
$photo1_target_path = "../users/" . $clean_owner . "/images/"; 

// Creates image file name //
$photo1_target_path = $photo1_target_path . basename( $_FILES['file1']['name']);
if(move_uploaded_file($_FILES['file1']['tmp_name'], $photo1_target_path)) {
	$photo1_filename = basename( $_FILES['file1']['name']);
	$clean_filenames = trim($photo1_filename);
	$clean_mainphoto = trim($photo1_filename);
} else {
	$_SESSION['error_top'] = "There was a problem uploading your photos";
	$_SESSION['error_field'] = "file1";
	header("Location: $backlink");

So what part of that holds the info that if passed back to the file box value field, will point to the right file when they re-submit the form? Is it like this?

$_SESSION[‘tmpFile1’] = basename( $_FILES[‘file1’][‘name’]);

Did you try?

LOL yeah, sorry…

I tried various forms of the array, like $_SESSION[‘tmpFile1’] = $_FILES[‘file1’] and with the different array parts but nothing works… but I was passing it back to the value=“” part of the file input field, should I be passing it back into the superglobal $_FILE by putting it into a session var in the controller and then putting it back into $_FILE on the view like $_FILES[‘file1’] = $_SESSION[‘tmpFile1’]; ???

For security reasons you cannot pre-populate the value of a FILE input field.

Yes, I know. But there has to be a way to take the value of what they selected the first time, and save it for when they re-submit the form, after fixing errors. Or else they have to re-selct their files again, know what I mean? So it isn’t really re-populating it but rather saving it… it seems that when you go back to the form the $_FILES array gets unset, right?

There isn’t.

That’s not what I wanted to hear.

Can you stick the values into a session variable and then use that for the upload code when they re-submit?

If you could do that, and maybe use JS to put the value in the file field, and then hide that field in some way from the user, then you could put any file name in there, and upload documents without the user even knowing it.
It would be quite a big security hole, wouldn’t it?

Maybe… I am having a lot of complaints from people who have errors on the form and then go back to fix them and forget to upload their pics, and have to go back and edit the post… they’re morons, they keep putting question marks in the price field etc…

I could upload pics before I validate but then I have strangler pics up there with no associated post. I want to save that value of the file for a minute while they fix their errors.

You could use JS to do a first, client side check of the form, before actually submitting the form. That way, those users that have JS enabled (and I guess it would be the larger part) wouldn’t have to choose the file again.

I already do that but the client (the guy paying me not the browser) requires a PHP validator class to catch those who fall thru the cracks w/o JS… I code OOP PHP all day and I hate this when clients demand you do this, but I need to find a solution for it. I figure 99% of people have JS on but we get ten whiners a week who say they added pics and paid for pics but they didn’t get uploaded.

Of course he does. You can’t rely on JS to do the validating, it’s just something extra. The final validation has always to be done server side.

I figure 99% of people have JS on but we get ten whiners a week who say they added pics and paid for pics but they didn’t get uploaded.

I don’t know. Try to make the error messages clearer? If there are any errors in the form, add a clear message stating that they’ll have to choose the file they want to upload again?
If they don’t choose the file again after correcting the mistakes they made in filling out the form, I guess another error message will appear telling them to choose a file?

You could also cut the form in two pieces. First have them insert all the info, and validate that. When all is ok, let them choose the file to upload.

Here’s a screenshot of the form. I actually replaced the file boxes with a graphic using jQuery, and hiding the file box but still populating it. When they select one file, another box appears, until there’s 4 total. It works great but I just have this issue of them forgetting to re-add pics.