简体   繁体   English

如何防止上传脚本在页面重新加载时再次移动文件?

[英]How to Prevent an Upload Script to Move the File AGAIN on Page Reload?

The Form: 表格:

<div id="maindiv">

            <div id="formdiv">
                <h2>Multiple Image Upload Form</h2>
                <form enctype="multipart/form-data" action="" method="post">
                    First Field is Compulsory. Only JPEG,PNG,JPG Type Image Uploaded. Image Size Should Be Less Than 100KB.
                    <hr/>
                    <div id="filediv"><input name="file[]" type="file" id="file"/></div><br/>

                    <input type="button" id="add_more" class="upload" value="Add More Files"/>
                    <input type="submit" value="Upload File" name="submit" id="upload" class="upload"/>
                </form>
                <br/>
                <br/>
                <!-------Including PHP Script here------>
                <?php include "upload.php"; ?>
            </div>

        </div>

The Script: 剧本:

<?php
if (isset($_POST['submit'])) {
    $j = 0; //Variable for indexing uploaded image

    $target_path = "uploads/"; //Declaring Path for uploaded images
    for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array

        $validextensions = array("jpeg", "jpg", "png");  //Extensions which are allowed
        $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.)
        $file_extension = end($ext); //store extensions in the variable

        $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];//set the target path with a new name of image
        $j = $j + 1;//increment the number of uploaded images according to the files in array

      if (($_FILES["file"]["size"][$i] < 100000) //Approx. 100kb files can be uploaded.
                && in_array($file_extension, $validextensions)) {
            if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {//if file moved to uploads folder
                echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
            } else {//if file was not moved.
                echo $j. ').<span id="error">please try again!.</span><br/><br/>';
            }
        } else {//if file size and file type was incorrect.
            echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
        }
    }
}
?>

This script also uses javascript for multiple image upload. 该脚本还使用javascript来上传多个图像。

When clicking on "page reload" in the web browser, after the first upload has been done, then the script assigns the same file a new ID code for the name and moves the image files a second time into the "uploads" folder. 当在Web浏览器中单击“页面重新加载”时, 完成第一次上传 ,脚本将为该文件分配一个新的ID名称代码 ,并将图像文件第二次移动到“上传”文件夹中。

How to prevent the script from moving the image files (with a new ID code assigned for the name) a second time into the "uploads" folder? 如何防止脚本移动图像文件(分配给该名称的新ID码) 第二次进入“上传”文件夹?

Any suggestions are much appreciated. 任何建议,不胜感激。

EDIT: 编辑:

So, I opted for a "redirect" to the same page, where the form gets cleared out , after the submission has been done. 因此,我选择了“重定向”到同一页面,提交完成后,表单将被清除

It looks like, this is the way to do it. 看起来,这就是这样做的方法。

Before upload (where you displays the upload FORM) you can create a key (random) and store it on your server somehere. 在上载之前(在其中显示上载表格),您可以创建密钥(随机)并将其存储在此处的服务器上。 (temp folder) (using a serial number is even more safe) You can include this as "hidden" value in the upload form. (临时文件夹)(使用序列号更为安全),您可以在上传表单中将其作为“隐藏”值。 The script, which is processing the upload, should look first for this key, if it exists, you can delete it and proceed with the rename / upload / whatever you want to do. 正在处理上载的脚本应首先查找该密钥,如果存在,则可以将其删除,然后继续进行重命名/上载/所需的操作。 If the key is gone, you can be sure it's a duplicated request and simply ignore it. 如果密钥不见了,您可以确定它是重复的请求,只需忽略它即可。

Don't forget to clean your temp folder every time (let's say delete every keys which is older than an hour or so) 别忘了每次都清理您的临时文件夹(例如删除所有早于一个小时左右的键)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM