Php – Unset uploaded files in PHP

csvformsPHPweb-applications

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?

Best Answer

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.