在PHP中取消设置上传的文件 [英] Unset uploaded files in PHP

查看:337
本文介绍了在PHP中取消设置上传的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,我用来接收上传的.csv文件,解析它,并将数据插入我的MySQL数据库在Apache服务器上。页面首先检查是否有上传的文件。如果有,它会处理数据,如果没有显示窗体(下面)。

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>

我的问题是,目前用户只需F5浏览器一遍又一遍,仍然在服务器和$ _FILES数组中,它每次都会处理它。

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.

我试过:

unlink($ _ FILES ['uploaded files'] ['tmp_name'])
unlink($ _ FILES ['uploadedfile'])
unset($ _ FILES ['uploadedfile'] ['tmp_name'])

unset($_FILES['uploadedfile'])`

甚至通过Javascript重置表单(我知道这不会工作,但它只是为了消除所有的疑问)。所有没有用。我相信这是一个简单的我缺少...它几乎总是。任何想法?

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?

推荐答案

这不是取消设置,因为post动作存储在浏览器的结尾,时间量,因为它只有一个csv)当他们击中F5。

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.

您可以这样做:

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.

这篇关于在PHP中取消设置上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆