2013-10-24 68 views
1

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

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

,并上传任何文件,但只转“GIF”,“JPEG”,“JPG”,“PNG”,“拉链” , “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("abcdefghijklmnopqrstuvwxyz", 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); 
?> 
+0

阅读这也http://meta.stackoverflow.com/help/someone-answers您还需要接受的答案。 –

回答

1

这是浏览器的功能。如果你犯了一个POST请求,然后按F5浏览器尝试重新提交POST请求,因此你的形象,你的表单数据将再次submited。

一种解决方案是先进行页面的刷新从PHP作为GET请求。

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

这只能在没有内容已经被发送到输出缓冲区。命令后的退出对停止脚本执行也非常重要。

+0

不工作!如何工作? – user2719452

+0

什么exaclty问题?你能告诉我你是如何尝试的吗? – andreashager

0

唯一的办法是刷新页面文件已被上传后。添加以下代码刚过move_uploaded_file($ _ FILES [ “UploadedFile的”] [ “tmp_name的值”],$上传);

$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>'; 
} 

这样用户也将得到文件上传消息。

+0

致Downvoter:为什么? – Subin