根据目录中的现有文件在上传时顺序重命名文件
P粉310931198
P粉310931198 2023-09-10 13:14:12
0
1
404

我正在使用我在网上找到的这个(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, "
"; } } 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学习者快速成长!