dsagzmosl32217092 2011-10-10 23:13
浏览 47
已采纳

php文件重命名循环

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

But sometimes 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?

  • 写回答

1条回答 默认 最新

  • dqwh1205 2011-10-10 23:23
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?