在 PHP 中取消设置上传的文件
我有一个表单,用于接收上传的 .csv 文件、解析它并将数据插入到 Apache 服务器上的 MySQL 数据库中。页面首先检查是否有上传的文件。如果有,则处理数据,如果没有,则显示表格(如下)。
<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 数组中,因此每次都会处理它。
我尝试过:取消链接($_FILES['uploadedfile']['tmp_name'])
, 取消链接($_FILES['uploadedfile'])
, unset($_FILES['uploadedfile']['tmp_name'])
,
unset($_FILES['uploadedfile'])`
我什至通过Javascript重置了表单(我知道这行不通,但这样做只是为了消除所有疑问) 。一切都无济于事。我确信我错过了一些简单的东西......它几乎总是如此。有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不会取消设置,因为发布操作存储在浏览器端,并在按下 F5 时重新上传(需要很短的时间,因为它只是一个 csv)。这与他们使用表单上传另一个 csv 本质上相同。
您可以这样做:
这将清除先前请求中发送的发布数据。刷新不会重新提交表单。
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:
This will clear the post data sent in the earlier request. Refreshing will not resubmit the form.
您可以在处理后
header
将它们重定向到该上传页面,以防止发布数据通过刷新不断进入。但处理完成后应清除临时文件。除非您使用move_uploaded_file
函数,否则 PHP 不会保留该文件。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 themove_uploaded_file
function.