【发布时间】:2016-05-12 13:42:50
【问题描述】:
我正在创建一个表单来将照片上传到比赛。我在桌面版本中没有问题,但智能手机的问题是每张照片都加载为“image.jpeg”或“image.jpg”,因此阻止了下一张的上传,因为它检测到同名的图像。我怎样才能克服这个问题? 这是我的代码:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// 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 is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "Il file selezionato non è un'immagine. ";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Spiacente, una foto con lo stesso nome è già stata inviata. ";
$uploadOk = 0;
}
// Check file size 500kb
if ($_FILES["fileToUpload"]["size"] > 2000000) {
echo "Spiacente, le dimesioni della foto superano il limite consentito. ";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Spiacente, i formati consentiti sono jpg e png. ";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Spiacente la tua foto non è stata inviata. ";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
@mail($email_to, $email_subject, $email_message, $headers);
echo "Messaggio Inviato. La tua foto ". basename( $_FILES["fileToUpload"]["name"]). " è stata inviata con successo. ";
} else {
echo "Spiacente, si è verificato un errore nel caricamento della tua foto. ";
}
}
?>
【问题讨论】:
-
您可以删除检查文件是否已存在的 if 条件?但是如果你这样做,你应该随机生成一个文件名。
-
为每张图片命名。
-
但是如果我在用户上传照片时删除了 if ,会覆盖之前已经加载的内容,对吧?
-
@NanaPartykar 我该怎么办?
-
将
time()附加到image name。将$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);更改为$target_file = $target_dir.time().basename($_FILES["fileToUpload"]["name"]);
标签: php forms image-uploading