开发者

How to sequentially rename files (in PHP) that carry on from a sequence of already existing files?

开发者 https://www.devze.com 2023-02-11 19:11 出处:网络
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开发者_如何学C.jpg

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开发者_如何学C.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 ?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号