简体   繁体   English

如果存在,PHP重命名文件名将数字附加到结尾

[英]PHP Rename File name if Exists Append Number to End

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.jpg and it already exists I want to rename it as test1.jpg and then test2.jpg and so on.我正在尝试在上传图像时重命名图像的文件名(如果存在),比如我的文件名是test.jpg并且它已经存在我想将其重命名为test1.jpg然后test2.jpg等等。 With the code I've written its changing my file name like so test1.jpg and then test12.jpg any advice on fixing this would be great thank!使用我编写的代码更改我的文件名,例如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++;
}

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++;
}

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";
}

There are several ways for renaming image in PHP before uploading to the server. 在上传到服务器之前,有几种方法可以在PHP中重命名图像。 appending timestamp, unique id, image dimensions plus random number etc.You can see them all here 附加时间戳,唯一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 检查更多图像重命名策略

I checked SO and found a nice C# answer here , so I ported it for PHP:我检查了 SO 并在这里找到了一个不错的 C# 答案,所以我将它移植到 PHP:

$count = 0;
while (file_exists($filePath) === true) {
    if ($count === 0) {
        $filePath = str_replace($extension, '[' . ++$count . ']' . ".$extension", $filePath);
    } else {
        $filePath = str_replace("[$count].$extension", '[' . ++$count . ']' . ".$extension", $filePath);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM