刷新页面后发布的文件[重复]

Posted

技术标签:

【中文标题】刷新页面后发布的文件[重复]【英文标题】:Files being posted after refreshing page [duplicate] 【发布时间】:2017-03-16 05:51:44 【问题描述】:

编辑:标记为重复,但是当我尝试时,帖子中的解决方案对我没有帮助。现在不想用ajax做,所以我只是用window.location来重定向。我丢失了上传文件的名称,但我宁愿以某种方式处理这些文件。

我有一个上传文件的表单,我发布到同一页面,提交后文件被上传,但如果我刷新文件会重新上传。我尝试在上传脚本的末尾将 $_POST 和 $_FILES 设置为空白数组,但每次仍然保留发布数据.. 还尝试添加标题,但它说它们已经发送,当我尝试在脚本开头使用 ob_start ,没有变化。

我的表格和表格是这样的

<table name="doctor_file_table" >
            <tr>
                <td><b>Name</b></td>
                <td><b>Type</b></td>
                <td><b>Size (bytes)</b></td>
                <td><b>Created</b></td>
               <td><a href='path_to_file' download =''>Download</a></td>
                <td><button id ='id#' onClick='deleteFile(this)' type='button'>Delete</button></td>
            </tr>

<form action="" enctype="multipart/form-data" method="post">
    <div>
        <label for="upload">Add Attachments:</label>
        <input id="upload" name="upload[]" type="file" multiple="multiple"/>
    </div>
        <p><input type="submit" name="submit" value="Submit"></p>
    </form>'

这里是上传脚本:

if(isset($_POST['submit']) && $_POST['uploaded'] == 1)
echo $_POST['uploaded'];
if(count($_FILES['upload']['name']) > 0)
    //Loop through each file
    for($i=0; $i<count($_FILES['upload']['name']); $i++) 
      //Get the temp file path
        $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

        //Make sure we have a filepath
        if($tmpFilePath != "")

            //save the filename
            $shortname = $_FILES['upload']['name'][$i];
            //save the url and the file
            $filePath = "/var/www/html/doctor_files/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];
            $fullname = substr($filePath,27);
            //Upload the file into the temp dir
            if(move_uploaded_file($tmpFilePath, $filePath)) 

                $files[] = $shortname;
            $sql = 'insert into '.TABLE_DOCTOR_FILES.'(shortname,fullname,filepath,type,size,doctor_id) VALUES("'.$shortname.'", "'.$fullname.'", "'.$filePath.'", "'.$_FILES["upload"]["type"][$i].'",'.$_FILES["upload"]["size"][$i].',"'.$doctor_id.'")';                    
            database_void_query($sql);

             //use $shortname for the filename
                //use $filePath for the relative url to the file

            
          
    


//show success message
echo "<h1>Uploaded:</h1>";    

if(is_array($files))
    echo "<ul>";
    foreach($files as $file)
        echo "<li>$file</li>";
    
    echo "</ul>";

【问题讨论】:

当你按f5时,页面即将再次发送POST请求。你应该使用ajax。阅读关于 ajax on MDN 是的,我认为有一种简单的方法可以清除数据,但我想还是这样更好。 【参考方案1】:

请求标头将存储您的数据。因此,如果您刷新,数据将再次发送回来。

您有 3 个解决方案:

    将您的代码分成 2 个不同的页面

    使用ajax(当然这需要像no 1那样拆分页面)

    尝试重定向到另一个页面,然后再次重定向到您的表单页面。

要使用第三种方式,你可以试试这个:

index.php

<html>
<head><title>asdas</title></head>
<body><!--i give you header to know this is will give error header or not-->
<?php
if(isset($_POST['submit']) && $_POST['uploaded'] == 1)
echo $_POST['uploaded'];
if(count($_FILES['upload']['name']) > 0)
    //Loop through each file
    for($i=0; $i<count($_FILES['upload']['name']); $i++) 
      //Get the temp file path
        $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

        //Make sure we have a filepath
        if($tmpFilePath != "")

            //save the filename
            $shortname = $_FILES['upload']['name'][$i];
            //save the url and the file
            $filePath = "/var/www/html/doctor_files/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];
            $fullname = substr($filePath,27);
            //Upload the file into the temp dir
            if(move_uploaded_file($tmpFilePath, $filePath)) 

                $files[] = $shortname;
            $sql = 'insert into '.TABLE_DOCTOR_FILES.'(shortname,fullname,filepath,type,size,doctor_id) VALUES("'.$shortname.'", "'.$fullname.'", "'.$filePath.'", "'.$_FILES["upload"]["type"][$i].'",'.$_FILES["upload"]["size"][$i].',"'.$doctor_id.'")';                    
            database_void_query($sql);

             //use $shortname for the filename
                //use $filePath for the relative url to the file

            
          
    


//show success message
echo "<h1>Uploaded:</h1>";    
header('Location: http://localhost/***/success.php');
if(is_array($files))
    echo "<ul>";
    foreach($files as $file)
        echo "<li>$file</li>";
    
    echo "</ul>";


?>


<table name="doctor_file_table" >
            <tr>
                <td><b>Name</b></td>
                <td><b>Type</b></td>
                <td><b>Size (bytes)</b></td>
                <td><b>Created</b></td>
               <td><a href='path_to_file' download =''>Download</a></td>
                <td><button id ='id#' onClick='deleteFile(this)' type='button'>Delete</button></td>
            </tr>

<form action="" enctype="multipart/form-data" method="post">
    <div>
        <label for="upload">Add Attachments:</label>
        <input id="upload" name="upload[]" type="file" multiple="multiple"/>
    </div>
        <p><input type="submit" name="submit" value="Submit"></p>
    </form>

成功.php

<html><head><title>redirect</title></head>
<body><!--i give you header to know this is will give error header or not-->
<?php
echo "<p>Your upload is success</p>";   
echo "<p>You will redirect back</p>";
echo "<p><a href=\"index.php\">or press this to redirect directly</a></p>";
?>
<script>
setTimeout(function () 
       window.location.href = "index.php"; 
    , 3000);

</script>
</body></html>

【讨论】:

以上是关于刷新页面后发布的文件[重复]的主要内容,如果未能解决你的问题,请参考以下文章

php如何避免刷新页面重复提交

AngularJS关于带有html5mode页面刷新错误的页面[重复]

php 如何避免刷新页面重复插入数据到数据库

页面刷新重复提交

PHP避免刷新页面重复提交

单击链接时刷新页面[重复]