开发者

php file renaming loop

开发者 https://www.devze.com 2023-04-12 06:27 出处:网络
I run a ecommerce site, and I have tons of product images. The naming rules are simple: productid-picnumber.

I run a ecommerce site, and I have tons of product images. The naming rules are simple: productid-picnumber.

But sometime开发者_如何转开发s theres gaps betweeen the picnumbers.

Example for the pictures for product id 4519:

4519-0.jpg
4519-3.jpg
4519-4.jpg
4519-5.jpg
4519-8.jpg

I'm trying to write an algorithm to rename the pictures. The pictures from this product should be renamed like this:

4519-0.jpg
4519-1.jpg
4519-2.jpg
4519-3.jpg
4519-4.jpg

In order. 0 must always be 0, as it's the main product image. -0 is always there. Let's say $ids is an array containing all my product ids.

Is looping through the product ids and using file_exists() the best way?


I highly suggest you that you get all filenames from a directory listing first, and then run the renaming "dry" (map oldname -> new name for the files to change) and then output if the actual result of the operation is the expected result.

This will not only save you much file_exists checks but also keeps things more safely.

Additionally you can think of using some standard file-system utilities which can rename/renumber files quite quickly. Check your OS documentation.

0

精彩评论

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

关注公众号