【问题标题】:PHP Rename File name if Exists Append Number to End如果存在,PHP重命名文件名将数字附加到结尾
【发布时间】:2013-04-14 17:30:55
【问题描述】:

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

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

【问题讨论】:

  • 清除变量 $actual_name 曾经使用过,因为它是一个循环,通过执行一些类似的操作,$acutal_name = ''
  • 您可以在while 循环中使用$i 测试名称......就像在"tmp/".$actual_name.$i.".".$extension 中一样,并在while 循环$name = "tmp/".$actual_name.$i.".".$extension 之后创建值$name
  • 我通常不是那种人,但你真的应该考虑尝试我的答案并使其成为接受的答案。祝你有美好的一天:)

标签: php file-upload upload rename file-exists


【解决方案1】:

我检查了 SO 并找到了一个不错的 C# 答案 here,所以我将它移植到 PHP:

['extension' => $extension] = pathinfo($filePath);
$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);
    }
}

【讨论】:

  • $extension 变量未声明。
【解决方案2】:

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

示例:

$name = "foo.bar";
$path = 'C:/Users/hp/Desktop/ikreports';
for ($i=1; $i<=10; $i++) {
  $newName = newName($path, $name);
  file_put_contents($newName, 'asdf');
}

新版本(2022):

function newName2($fullpath) {
  $path = dirname($fullpath);
  if (!file_exists($fullpath)) return $fullpath;
  $fnameNoExt = pathinfo($fullpath,PATHINFO_FILENAME);
  $ext = pathinfo($fullpath, PATHINFO_EXTENSION);

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

用法:

for ($i=1; $i<=10; $i++) {
  $newName = newName2($fullpath);
  file_put_contents($newName, 'asdf');
}

【讨论】:

  • 看起来这个答案总是附加'(1)',所以: foo.bar -> foo (1).bar -> foo (1) (1).bar 等等。从技术上讲是可行的,但任何以前处理过文件的人都会期望,这个数字会增加 (foo (2).bar) 而不仅仅是追加。
  • “看起来像”?你有没有尝试过?也许你用错了。
  • 我自己提供了一个答案,你应该可以看到区别。
【解决方案3】:

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

首先,检查托管图像文件夹中是否存在图像文件名,否则将其上传。 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);

}

查看更多图片renaming tactics

【讨论】:

    【解决方案4】:

    这是我认为应该做的小修改:

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

    【讨论】:

    • 工作就像一个魅力。你是我的英雄
    • 看起来这个答案总是附加 1,所以: foo.bar -> foo1.bar -> foo11.bar 等。它在技术上是有效的,但任何以前处理过文件的人都会期望,数字增加(foo2.bar)而不仅仅是追加。
    猜你喜欢
    • 2015-03-18
    • 2014-08-09
    • 2016-07-20
    • 2018-11-19
    • 2017-09-11
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多