views:

23

answers:

2

I have a Form that I am using to receive an uploaded .csv file, parse it and insert the data into my MySQL db on an Apache server. The page first checks to see if there is an uploaded file. If there is, it processes the data, if not the form (below) is displayed.

<form enctype="multipart/form-data" action="uploadfaculty.php" method="POST" id="UploadForm">
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

My problem is that currently the user can simply F5 the browser over and over again and because the file is still on the server and in the $_FILES array, it processes it every time.

I've tried:
unlink($_FILES['uploadedfile']['tmp_name']), unlink($_FILES['uploadedfile']), unset($_FILES['uploadedfile']['tmp_name']), and

unset($_FILES['uploadedfile'])`

I've even reset the form via Javascript (which I knew would not work, but did it just to eliminate all doubt). All to no avail. I'm sure it's something simple I'm missing...it almost always is. Any thoughts?

+2  A: 

You can header redirect them to that upload page after processing to prevent the post data from continually going in via a refresh. But the temporary file should be cleared once the processing is done. PHP does not keep the file unless you use the move_uploaded_file function.

Brad F Jacobs
What exactly do you mean by 'the temporary file should be cleared once the processing is done'? Do you mean to say that once my script runs its course and I fclose($handle) it should be gone? If so, then something else fishy must be going on if I can just refresh my browser and the data can be found, parsed, and inserted all over again. Here's a test page: http://studentorg.ecpi.edu/oswm/uploadcsv.php
d2burke
The `header("location:thankyou.php")` is a good one though, I'd just rather not have to do that is there is another way.
d2burke
The $_FILES array isn't populated by files users have uploaded with previous requests. If they are hitting F5 and refreshing then the file is being uploaded every time. You need to send them to a different page to prevent the request from being resubmitted. That doesn't keep them from using back, though.
Inigoesdr
What I mean is that there should only ever be one copy of that file on the server. When PHP is done processing that temporary file is cleared and deleted. It just means it will not take up your disk space. If they do keep pressing refresh however, it will eat up your bandwidth. So a redirect to a thank you page or just to that same upload page itself should clear the POST data.
Brad F Jacobs
Ok, thanks for the clarification on the temporary file storage, I figured that's what you meant. I'd like to stay on the same page though
d2burke
+2  A: 

It's not unsetting because the post action is stored on the browser's end and being re-uploaded (in a small amount of time as it's only a csv) when they hit F5. Which is essentially the same as them using the form to upload another csv.

You can do this:

if (isset($_POST['csv'])){
 $DataProcessed = DataProcessingFunction();
}

if (isset($DataProcessed) && $DataProcessed){
  header("Location: /path/to/form/page.php");
  exit();
}

This will clear the post data sent in the earlier request. Refreshing will not resubmit the form.

Mahdi.Montgomery
Perfect. That did it.
d2burke