1

为什么刷新页面后重新提交

http://qass.im/message-envelope/

并上传任何文件,但只有分机“gif”、“jpeg”、“jpg”、“png”、“zip”、“pdf”、“docx”、“rar”、“txt”

上传后点击F5按钮刷新页面

现在重新提交并再次上传文件!

为什么?

我想在没有 jquery 的情况下上传文件后禁用重新提交

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png", "zip", "pdf", "docx", "rar", "txt", "doc");
$temp = explode(".", $_FILES["uploadedfile"]["name"]);
$extension = end($temp);
$newname = $extension.'_'.substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 7)), 4, 7);
$imglink = 'attachment/attachment_file_';
$uploaded = $imglink .$newname.'.'.$extension;
if ((($_FILES["uploadedfile"]["type"] == "image/jpeg")
|| ($_FILES["uploadedfile"]["type"] == "image/jpeg")
|| ($_FILES["uploadedfile"]["type"] == "image/jpg")
|| ($_FILES["uploadedfile"]["type"] == "image/pjpeg")
|| ($_FILES["uploadedfile"]["type"] == "image/x-png")
|| ($_FILES["uploadedfile"]["type"] == "image/gif")
|| ($_FILES["uploadedfile"]["type"] == "image/png")
|| ($_FILES["uploadedfile"]["type"] == "application/msword")
|| ($_FILES["uploadedfile"]["type"] == "text/plain")
|| ($_FILES["uploadedfile"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|| ($_FILES["uploadedfile"]["type"] == "application/pdf")
|| ($_FILES["uploadedfile"]["type"] == "application/x-rar-compressed")
|| ($_FILES["uploadedfile"]["type"] == "application/x-zip-compressed")
|| ($_FILES["uploadedfile"]["type"] == "application/zip")
|| ($_FILES["uploadedfile"]["type"] == "multipart/x-zip")
|| ($_FILES["uploadedfile"]["type"] == "application/x-compressed")
|| ($_FILES["uploadedfile"]["type"] == "application/octet-stream"))
&& ($_FILES["uploadedfile"]["size"] < 5242880) // Max size is 5MB
&& in_array($extension, $allowedExts))
{   
move_uploaded_file($_FILES["uploadedfile"]["tmp_name"],
$uploaded );
echo '<a target="_blank" href="'.$uploaded.'">click</a>';
echo '<h3>'.$uploaded.'</h3>';
}
if($_FILES["uploadedfile"]["error"] > 0){
echo '<h3>Please choose file to upload it!</h3>'; // If you don't choose file
}
elseif(!in_array($extension, $allowedExts)){
echo '<h3>This extension is not allowed!</h3>'; // If you choose file not allowed
}
elseif($_FILES["uploadedfile"]["size"] > 5242880){
echo "Big size!"; // If you choose big file
}

    unset($_FILE); //add these two lines
    unset($_REQUEST);
?>
4

2 回答 2

1

这是您的浏览器的一项功能。如果您发出 POST 请求并点击 F5,浏览器会尝试重新提交 POST 请求,因此您的图像和表单数据将再次提交。

一种解决方案是您从 PHP 作为 GET 请求刷新页面。

header('Location: /yoururl'); // May use $_SERVER['REQUEST_URI'] ;)
exit;

这仅在没有内容发送到输出缓冲区时才有效。命令后的退出对于停止脚本执行也很重要。

于 2013-10-24T13:07:00.197 回答
0

唯一的方法是在文件上传后刷新页面。在move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], $uploaded );之后添加以下代码

$page = "http://yoururl.com/upload.php?uploaded&url=".$uploaded;
$sec = "1";
header("Location: $page");

并在问题的PHP代码末尾添加以下代码:

if(isset($_GET['uploaded'])){
 $uploaded=$_GET['url'];
 echo '<a target="_blank" href="'.$uploaded.'">click</a>';
 echo '<h3>'.$uploaded.'</h3>';
}

这样用户也会收到文件上传的消息。

于 2013-10-24T14:55:57.290 回答