我写的php代码上传图像从客户端到主机和网站,我的问题是,如果我刷新页面,最后一个文件将再次上传,它将被复制,如果再次刷新这个问题再次发生
我的代码:
<?php
$target_dir = 'C:\xampp\htdocs\upload\upload\jpg\\';
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$filename=basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$move=$_FILES["fileToUpload"]["tmp_name"];
$isss=1;
// Check if image file is a actual image or fake image
// if(isset($_POST["submit"])) {
// $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
// if($check !== false) {
// echo "File - " . $check["mime"] . ".";
// $uploadOk = 1;
// } else {
// echo "File is not an image.";
// $uploadOk = 0;
// }
// }
$time =date("Y-m-d-h-i-sa");
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" ) {
echo "jpg png jpeg mojaz hast";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Upload nashod";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($move, $target_file) ) {
echo "<div class=dvs>". "File: ". basename( $_FILES["fileToUpload"]["name"]). " Upload shod !"."</div>";
rename("$target_file",$target_dir.$time.".jpg");
}
else {
echo "moshkeli dar upload shoma hast.";
}
// foreach(glob('jpg/*.jpg') as $filename1){
// echo $filename1;
// }
}
?>
1条答案
按热度按时间mspsb9vt1#
出现此问题是因为您停留在处理post请求的页面上。您需要做的是重定向到另一个页面或从中上载的页面,并在那里显示上载结果。
您可以使用
header
功能如下: