如何按顺序重命名从一系列已存​​在文件开始的文件(在 PHP 中)?

发布于 2024-10-18 06:22:09 字数 546 浏览 11 评论 0原文

假设我有一个源文件夹,其中包含一堆文件,如下所示

0_0.jpg
0_1.jpg
0_2.jpg
0_3.jpg
1_0.jpg
1_1.jpg
1_2.jpg
1_3.jpg
2_0.jpg
2_1.jpg
2_2.jpg
2_3.jpg

,然后我有一个目标文件夹,其中包含完全相同的文件名。

如何将源文件夹中的文件添加到目标文件夹中,以便文件名如下所示:

0_0.jpg
0_1.jpg
0_2.jpg
0_3.jpg
1_0.jpg
1_1.jpg
1_2.jpg
1_3.jpg
2_0.jpg
2_1.jpg
2_2.jpg
2_3.jpg
---------- files from the source folder from now
3_0.jpg
3_1.jpg
3_2.jpg
3_3.jpg
4_0.jpg
4_1.jpg
4_2.jpg
4_3.jpg
5_0.jpg
5_1.jpg
5_2.jpg
5_3.jpg

Is there a approximation to this in PHP ?

Say I have a source folder with a bunch of files in it like this

0_0.jpg
0_1.jpg
0_2.jpg
0_3.jpg
1_0.jpg
1_1.jpg
1_2.jpg
1_3.jpg
2_0.jpg
2_1.jpg
2_2.jpg
2_3.jpg

And then I have a target folder that has the exact same filenames in it.

How can I add the files from the source folder to the target folder so the filenames come out like this:

0_0.jpg
0_1.jpg
0_2.jpg
0_3.jpg
1_0.jpg
1_1.jpg
1_2.jpg
1_3.jpg
2_0.jpg
2_1.jpg
2_2.jpg
2_3.jpg
---------- files from the source folder from now
3_0.jpg
3_1.jpg
3_2.jpg
3_3.jpg
4_0.jpg
4_1.jpg
4_2.jpg
4_3.jpg
5_0.jpg
5_1.jpg
5_2.jpg
5_3.jpg

Is there a reasonable way to to this in PHP ?

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

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

发布评论

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

评论(1

冷弦 2024-10-25 06:22:09

使用 scandir (http://php.net/scandir) 循环遍历文件并获取第一个文件中的最后一个文件目录。解析出数字以确定第二个目录的起始路径。然后循环遍历第二个目录中的文件并重命名(http://php.net/rename)。

编辑:

根据@Toby在评论中的建议,

Split('_',$var) 将返回一个数组
字符串中的项目数按 _ 划分
允许轻松访问两个
文件名中的数字

Use scandir (http://php.net/scandir) to loop through files and get the last file in the first directory. Parse out the numbers to determine the starting path for the second directory. Then loop through the files in the second directory and rename (http://php.net/rename).

Edit:

As suggested by @Toby in the comments,

Split('_',$var) will return an array
of items in a string divided on _
allowing easy access to the two
numbers in the filename

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