【问题标题】:php check file name exist, rename the filephp检查文件名是否存在,重命名文件
【发布时间】:2012-04-18 09:41:31
【问题描述】:

如何检查文件名是否存在,重命名文件?

比如我上传了一张图片1086_002.jpg,如果文件存在,重命名文件为1086_0021.jpg并保存,如果1086_0021.jpg存在,重命名1086_00211.jpg并保存,如果1086_00211.jpg存在,重命名1086_002111.jpg 并保存...

这是我的代码,它只有在1086_002.jpg存在的情况下才可以,将文件重命名为1086_0021.jpg,也许应该做一个foreach,但是怎么做?

//$fullpath = 'images/1086_002.jpg';

if(file_exists($fullpath)) {
    $newpieces = explode(".", $fullpath);
    $frontpath = str_replace('.'.end($newpieces),'',$fullpath);
    $newpath = $frontpath.'1.'.end($newpieces);
}

file_put_contents($newpath, file_get_contents($_POST['upload']));

【问题讨论】:

  • php 文件上传不通过 $_POST。它们通过 $_FILES 并具有与任何其他表单字段非常不同的处理语义。

标签: php file-exists


【解决方案1】:

尝试类似:

$fullpath = 'images/1086_002.jpg';
$additional = '1';

while (file_exists($fullpath)) {
    $info = pathinfo($fullpath);
    $fullpath = $info['dirname'] . '/'
              . $info['filename'] . $additional
              . '.' . $info['extension'];
}

【讨论】:

  • +1,我本来打算写一些类似的东西,但这已经是最好的了。
  • @Baba 你是怎么得出这个结论的? $fullpath 在 while 循环中更新(注意每次添加的 $additional)。这里无限循环的唯一方法是无限数量的文件。
  • 你应该总是增加 $additional ???例如。 $additional++ ... 因为$additional 只是1 并且它不会改变.. 脚本会run till it times out 谢谢
  • $additional 每次都附加,而不是递增。根据 OP 的要求。你有没有试过running the code
【解决方案2】:

为什么不在文件名上附加一个时间戳呢?这样您就不必担心多次上传的文件的任意长文件名了。

【讨论】:

    【解决方案3】:

    希望对你有帮助

    $fullPath = "images/1086_002.jpg" ;
    $fileInfo = pathinfo($fullPath);
    list($prifix, $surfix) = explode("_",$fileInfo['filename']);
    $x = intval($surfix);
    $newFile = $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $prifix. "_" . str_pad($x, 2,"0",STR_PAD_LEFT)  . $fileInfo['extension'];
    while(file_exists($newFile)) {
        $x++;
        $newFile = $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $prifix. "_" . str_pad($x, 2,"0",STR_PAD_LEFT)  . $fileInfo['extension'];
    }
    
    file_put_contents($newFile, file_get_contents($_POST['upload']));
    

    希望对你有帮助

    谢谢

    :)

    【讨论】:

      【解决方案4】:

      我觉得这样会更好。它将有助于跟踪同名文件的上传次数。它的工作方式与 Windows 操作系统在找到具有相同名称的文件时重命名文件的方式相同。

      工作原理:如果 media 目录有一个名为 002.jpg 的文件,而您尝试上传一个同名文件,它将被保存为 002(1).jpg 再次尝试上传同一文件会将新文件另存为 002(2).jpg

      希望对你有帮助。

      $uploaded_filename_with_ext = $_FILES['uploaded_image']['name'];
      $fullpath = 'media/' . $uploaded_filename_with_ext;
      $file_info = pathinfo($fullpath);
      $uploaded_filename = $file_info['filename'];
      
      $count = 1;                 
      while (file_exists($fullpath)) {
        $info = pathinfo($fullpath);
        $fullpath = $info['dirname'] . '/' . $uploaded_filename
        . '(' . $count++ . ')'
        . '.' . $info['extension'];
      }
      $image->save($fullpath);
      

      【讨论】:

        【解决方案5】:

        您可以将if 语句更改为while 循环:

        $newpath = $fullpath;
        while(file_exists($newpath)) {
            $newpieces = explode(".", $fullpath);
            $frontpath = str_replace('.'.end($newpieces),'',$fullpath);
            $newpath = $frontpath.'1.'.end($newpieces);
        }
        
        file_put_contents($newpath, file_get_contents($_POST['upload']));
        

        【讨论】:

        • 我只是使用了OP提供的。
        猜你喜欢
        • 2012-11-07
        • 2013-08-14
        • 1970-01-01
        • 1970-01-01
        • 2012-03-06
        • 2023-03-05
        • 2015-03-18
        • 2023-01-20
        • 2014-09-09
        相关资源
        最近更新 更多