PHP 重命名文件名(如果存在)追加数字到结尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16136519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:34:57  来源:igfitidea点击:

PHP Rename File name if Exists Append Number to End

phpfile-uploaduploadrenamefile-exists

提问by user2201765

I'm trying to rename the file name of an image when it's uploaded if it exists, say if my file name is test.jpgand it already exists I want to rename it as test1.jpgand then test2.jpgand so on. With the code I've written its changing my file name like so test1.jpgand then test12.jpgany advice on fixing this would be great thank!

我正在尝试在上传图像时重命名图像的文件名(如果存在),比如说我的文件名是否test.jpg存在并且它已经存在我想将其重命名为test1.jpg然后test2.jpg等等。使用我编写的代码,它会像这样更改我的文件名test1.jpg,然后test12.jpg任何有关解决此问题的建议将非常感谢!

PHP

PHP

$name = $_FILES['picture']['name'];
$actual_name = pathinfo($name,PATHINFO_FILENAME);
$extension = pathinfo($name, PATHINFO_EXTENSION);

$i = 1;
while(file_exists('tmp/'.$actual_name.".".$extension))
{           
    $actual_name = (string)$actual_name.$i;
    $name = $actual_name.".".$extension;
    $i++;
}

回答by Jason

Here's a minor modification that I think should do what you want:

这是我认为应该做你想做的一个小修改:

$actual_name = pathinfo($name,PATHINFO_FILENAME);
$original_name = $actual_name;
$extension = pathinfo($name, PATHINFO_EXTENSION);

$i = 1;
while(file_exists('tmp/'.$actual_name.".".$extension))
{           
    $actual_name = (string)$original_name.$i;
    $name = $actual_name.".".$extension;
    $i++;
}

回答by Fandi Susanto

Inspired from @Jason answer, i created a function which i deemed shorter and more readable filename format.

受@Jason 回答的启发,我创建了一个函数,我认为它的文件名格式更短、更易读。

function newName($path, $filename) {
    $res = "$path/$filename";
    if (!file_exists($res)) return $res;
    $fnameNoExt = pathinfo($filename,PATHINFO_FILENAME);
    $ext = pathinfo($filename, PATHINFO_EXTENSION);

    $i = 1;
    while(file_exists("$path/$fnameNoExt ($i).$ext")) $i++;
    return "$path/$fnameNoExt ($i).$ext";
}

回答by Daniel Nyamasyo

There are several ways for renaming image in PHP before uploading to the server. appending timestamp, unique id, image dimensions plus random number etc.You can see them all here

在上传到服务器之前,有几种方法可以在 PHP 中重命名图像。附加时间戳、唯一 ID、图像尺寸加上随机数等。你可以在这里看到它们

First, Check if the image filename exists in the hosted image folder otherwise upload it. The while loop checks if the image file name exists and appends a unique id as shown below ...

首先,检查图像文件名是否存在于托管图像文件夹中,否则上传。while 循环检查图像文件名是否存在并附加一个唯一的 id,如下所示...

function rename_appending_unique_id($source, $tempfile){

    $target_path ='uploads-unique-id/'.$source;
     while(file_exists($target_path)){
        $fileName = uniqid().'-'.$source;
        $target_path = ('uploads-unique-id/'.$fileName);
    }

    move_uploaded_file($tempfile, $target_path);

}

if(isset($_FILES['upload']['name'])){

    $sourcefile= $_FILES['upload']['name'];
    tempfile= $_FILES['upload']['tmp_name'];

    rename_appending_unique_id($sourcefile, $tempfile);

}

Check more image renaming tactics

查看更多图像重命名策略