【问题标题】:Skip the "Check if File Already Exists"跳过“检查文件是否已存在”
【发布时间】: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


【解决方案1】:

检查文件是否存在,如果存在,则创建一个带有时间的新文件名(或类似的东西),然后移动上传的文件。

下面给出了一个示例,您应该根据需要对其进行修改

    $file_name = $_FILES["file"]["name"];
    if (file_exists($dstFile . $file_name))  {
        $path_parts = pathinfo($dstFile . $file_name);
        $file_name = $path_parts['filename'] . '-' . time() . '.' . $path_parts['extension'];
    }
    $dstFile .= $file_name;
    move_uploaded_file($_FILES["file"]["tmp_name"],$dstFile)

【讨论】:

  • 但是如果文件存在,我必须保留当前文件吗?然后粘贴?
  • 您要覆盖旧文件吗?或者您想保留旧文件并为新文件创建新文件名?我提供的解决方案是针对第二个
猜你喜欢
  • 2020-06-14
  • 2020-10-19
  • 2013-10-28
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 2023-01-25
相关资源
最近更新 更多