在 PHP 中取消设置上传的文件

发布于 2024-09-18 11:02:44 字数 784 浏览 3 评论 0原文

我有一个表单,用于接收上传的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱要勇敢去追 2024-09-25 11:02:44

它不会取消设置,因为发布操作存储在浏览器端,并在按下 F5 时重新上传(需要很短的时间,因为它只是一个 csv)。这与他们使用表单上传另一个 csv 本质上相同。

您可以这样做:

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

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

这将清除先前请求中发送的发布数据。刷新不会重新提交表单。

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.

世界等同你 2024-09-25 11:02:44

您可以在处理后 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 the move_uploaded_file function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文