重命名同名文件

发布于 2024-08-25 15:43:14 字数 1285 浏览 10 评论 0原文

我使用以下函数来重命名缩略图。
例如,如果我将名为“image.png”的文件上传到上传文件夹,并且该文件夹中已有名为“image.png”的文件,则新文件会自动重命名为“image-copy-1.png” ”。如果还有一个名为“image-copy-1.png”的文件,它将被重命名为“image-copy-2.png”,依此类推。
以下函数返回新文件名。至少这是它应该做的......
不过,重命名似乎无法正常工作。有时它会产生奇怪的结果,例如:(我总是上传一个名为“1.png”的文件)
1-copy-1.png
1-copy-2.png
1-copy-2-copy-1.png
1-copy-2-copy-3.png

我希望你理解我的问题,尽管我的描述有点复杂......你能告诉我这里出了什么问题吗? (额外问题:正则表达式是做这种事情的正确工具吗?)

<?php
function renameDuplicates($path, $file)
{   
    $fileName = pathinfo($path . $file, PATHINFO_FILENAME);
    $fileExtension = "." . pathinfo($path . $file, PATHINFO_EXTENSION);

    if(file_exists($path . $file))
    {
        $fileCopy = $fileName . "-copy-1";

        if(file_exists($path . $fileCopy . $fileExtension))
        {           
            if ($contains = preg_match_all ("/.*?(copy)(-)(\\d+)/is", $fileCopy, $matches))
            {
                $copyIndex = $matches[3][0];            
                $fileName = substr($fileCopy, 0, -(strlen("-copy-" . $copyIndex))) . "-copy-" . ($copyIndex + 1);
            }                       
        }

        else
        {
            $fileName .= "-copy-1";
        }
    }

    $returnValue = $fileName . $fileExtension;  
    return $returnValue;
}?>

I use the following function to rename thumbnails.
For example, if I upload a file called "image.png" to an upload folder, and this folder already has a file named "image.png" in it, the new file automatically gets renamed to "image-copy-1.png". If there also is a file called "image-copy-1.png" it gets renamed to "image-copy-2.png" and so on.
The following function returns the new filename. At least that's what it is supposed to do...
The renaming doesn't seeem to work correctly, though. Sometimes it produces strange results, like: (I always uploaded a file named "1.png")
1-copy-1.png
1-copy-2.png
1-copy-2-copy-1.png
1-copy-2-copy-3.png

I hope you understand my problem, despite my description being somewhat complex... Can you tell me what went wrong here? (bonus question: Is regular expressions the right tool for doing this kind of stuff?)

<?php
function renameDuplicates($path, $file)
{   
    $fileName = pathinfo($path . $file, PATHINFO_FILENAME);
    $fileExtension = "." . pathinfo($path . $file, PATHINFO_EXTENSION);

    if(file_exists($path . $file))
    {
        $fileCopy = $fileName . "-copy-1";

        if(file_exists($path . $fileCopy . $fileExtension))
        {           
            if ($contains = preg_match_all ("/.*?(copy)(-)(\\d+)/is", $fileCopy, $matches))
            {
                $copyIndex = $matches[3][0];            
                $fileName = substr($fileCopy, 0, -(strlen("-copy-" . $copyIndex))) . "-copy-" . ($copyIndex + 1);
            }                       
        }

        else
        {
            $fileName .= "-copy-1";
        }
    }

    $returnValue = $fileName . $fileExtension;  
    return $returnValue;
}?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若无相欠,怎会相见 2024-09-01 15:43:14

更简单,无需正则表达式;

function renameDuplicates($path, $file)
{   
    $fileName = pathinfo($path . $file, PATHINFO_FILENAME);
    $fileExtension = "." . pathinfo($path . $file, PATHINFO_EXTENSION);

    $returnValue = $fileName . $fileExtension;

    $copy = 1;
    while(file_exists($path . $returnValue))
    {
        $returnValue = $fileName . '-copy-'. $copy . $fileExtension;
        $copy++;
    }
    return $returnValue;
}

性能问题的快速基准测试(32000 是文件夹中的最大文件数):

$start = microtime(1);
$c=0;
while($c<32000)
    if(file_exists(__FILE__))
        $c++;
echo microtime(1) - $start; /* 0.44202709197998 */

因此最坏情况下不到半秒。 100 份 - 0.0013940334320068 s。至于正则表达式:

$start = microtime(1);
$contains = preg_match_all ("/.*?(copy)(-)(\\d+)/is", __FILE__, $matches);
echo microtime(1) - $start; /* 0.010906934738159 */ 

因此,如果您计划拥有一个文件的超过 800 个副本,则正则表达式会更快(几微秒,但更快):)

Simpler, without regex;

function renameDuplicates($path, $file)
{   
    $fileName = pathinfo($path . $file, PATHINFO_FILENAME);
    $fileExtension = "." . pathinfo($path . $file, PATHINFO_EXTENSION);

    $returnValue = $fileName . $fileExtension;

    $copy = 1;
    while(file_exists($path . $returnValue))
    {
        $returnValue = $fileName . '-copy-'. $copy . $fileExtension;
        $copy++;
    }
    return $returnValue;
}

Quick benchmark for performance issues (32000 is max files in folder):

$start = microtime(1);
$c=0;
while($c<32000)
    if(file_exists(__FILE__))
        $c++;
echo microtime(1) - $start; /* 0.44202709197998 */

So less than half second for worst-case scenario. And for 100 copies - 0.0013940334320068 s. And as for regex:

$start = microtime(1);
$contains = preg_match_all ("/.*?(copy)(-)(\\d+)/is", __FILE__, $matches);
echo microtime(1) - $start; /* 0.010906934738159 */ 

so if you plan to have more than ~800 copies of one file regex will be faster (by some microseconds, but faster) :)

爱冒险 2024-09-01 15:43:14

当它重命名文件如 1-copy-2-copy-1.png 时,您上传的文件的名称是什么?

因为:
如果文件具有此名称1-copy-2.png,那么将1-copy-2-copy-1.png作为重命名文件是正常的...

否则有问题:)

when it rename the file like 1-copy-2-copy-1.png what is the name of the file you are uploading?

Because:
if the file has this name 1-copy-2.png it's normal to have 1-copy-2-copy-1.png as a renamed file...

otherwise there is a problem :)

早茶月光 2024-09-01 15:43:14

看起来您的代码只检查名为 1-copy-1.png 的文件副本是否存在?当它找到它时,它会将其重命名为 1-copy-2.png

但是它不会检查文件 1-copy-2.png 是否存在,也不会增加该文件。

但在您的解决方案中,脚本需要检查文件夹中的每个文件。那么如果文件很多的话。会很慢。

It looks like your code only checks if a file copy named 1-copy-1.png exists? And when it finds it it renames it to 1-copy-2.png

However it doesnt check if a file 1-copy-2.png exists and doesnt increment that.

But in your solution the script need to check every file in the folder. So if there are many files. It will be slow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文