重命名同名文件
我使用以下函数来重命名缩略图。
例如,如果我将名为“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更简单,无需正则表达式;
性能问题的快速基准测试(32000 是文件夹中的最大文件数):
因此最坏情况下不到半秒。 100 份 - 0.0013940334320068 s。至于正则表达式:
因此,如果您计划拥有一个文件的超过 800 个副本,则正则表达式会更快(几微秒,但更快):)
Simpler, without regex;
Quick benchmark for performance issues (32000 is max files in folder):
So less than half second for worst-case scenario. And for 100 copies - 0.0013940334320068 s. And as for regex:
so if you plan to have more than ~800 copies of one file regex will be faster (by some microseconds, but faster) :)
当它重命名文件如
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 have1-copy-2-copy-1.png
as a renamed file...otherwise there is a problem :)
看起来您的代码只检查名为 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.