根據目錄中的現有文件在上傳時順序重新命名文件
P粉310931198
P粉310931198 2023-09-10 13:14:12
0
1
566

我正在使用我在網路上找到的這個(php)上傳程式碼作為圖像上傳表單。

我意識到網站上已經有很多關於上傳時「重命名檔案」的問題/答案,我對它們進行了相當多的研究。

到目前為止......它們似乎都沒有具體解決我想做的事情。

希望這可以透過調整我正在使用的程式碼來實現。

最終...我希望能夠上傳單一文件(不是多個),並使用簡單的數字自動命名文件,例如; 1、2、3、4 或 5 等...

沒有前綴,也沒有副檔名。只是名稱的數值。

但是,我希望程式碼首先檢查目標目錄,以掃描哪些檔案「名稱」已存在。

因此,如果目錄中的現有檔案為 1、2、3、4 和 5...,新檔案將自動依序命名為 6,依此類推。

但是,如果目錄中的現有檔案為 1、2、4 和 5(不存在 3),則新檔案將作為 3 上傳,以保留順序。

或者,對於我的特殊需求,我甚至不介意目錄中的所有圖像是否都透過新上傳進行重命名。本質上是改變順序,從而保留數字序列。

這是我目前使用的上傳程式碼:

<?php

$target_dir = "userImages/userID/";

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$uploadOk = 1;

$imageFileType = strtolower(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 "File is not an image.";

    $uploadOk = 0;

  
  }
}

// Check if file already exists

if (file_exists($target_file)) {

  echo "Sorry, file already exists.";

  $uploadOk = 0;
}

// Check file size

if ($_FILES["fileToUpload"]["size"] > 500000) {

  echo "Sorry, your file is too large.";

  $uploadOk = 0;


}

// Allow certain file formats

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"

&& $imageFileType != "gif" ) {

  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";

  $uploadOk = 0;


}


// Check if $uploadOk is set to 0 by an error

if ($uploadOk == 0) {

  echo "Sorry, your file was not uploaded.";

// if everything is ok, try to upload file


} else {

  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

    echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has 
been uploaded.";

  } else {

    echo "Sorry, there was an error uploading your file.";

  
  }
}

?>

P粉310931198
P粉310931198

全部回覆(1)
P粉713866425

您可以嘗試這個函數,它會檢查目錄中的檔案是否連續命名,然後重新命名以使其始終編號,該函數會傳回新上傳檔案的下一個編號

function get_next_file_number($target_dir)  {

    $files = scandir($target_dir);
    if (false === $files) {         // If false then is not a directory, you could throw an Exception here
        return -1;
    }
    $counter = 0;
    unset($files[0], $files[1]);    // Delete the . and .. from array
    natsort($files);                // Important! Sort file names naturally
    foreach ($files as $file) {
        ++$counter;
        $file_info = pathinfo($file);
        $file_name = $file_info['filename'];
        $extension = $file_info['extension'];
        if ($file_name != $counter) {
            $old_name = $target_dir . DIRECTORY_SEPARATOR . $file;
            $new_name = $target_dir . DIRECTORY_SEPARATOR . $counter . "." . $extension;
            if (!file_exists($new_name)) {
                rename($old_name, $new_name);
            }
            else {
                // Oops trying to rename to a file that already exists, this shouldn't happen!
            }
            //echo "renamed: " . $old_name . " to " . $new_name, "<br>";
        }
    }
    return $counter + 1;
}

如何實現?

// .....
// You can check the returned value, if it is -1 you know there was an error
$next_number = get_next_file_number($target_dir);
$new_target_file = $target_dir . $next_number . "." . $imageFileType;
// Here you can make sure the file does not exists... just to make sure...
// Here move the uploaded file with the next number
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $new_target_file)) {
    echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
}
// .....
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!