20seven

Devigner blog about development and design

Renaming a Thumb

While working on migrating a shopping cart for a client to a newer cart I found that the product thumbnail naming conventions were different between carts. I also noticed they had just shy of 2500 images to be renamed, so within 5 minutes time they were all prefixed with thumb_. Here’s how I did it and hope someone else finds it useful.

<?php
$the_array = Array();
$handle = opendir(‘images/uploads/thumbs/’);

while (false !== ($file = readdir($handle))) {
&#xA0;&#xA0;&#xA0;if ($file != “.” && $file != “..”) {
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;$the_array[] = $file;
&#xA0;&#xA0;&#xA0;}
}
closedir($handle);

foreach ($the_array as $element) {
&#xA0;&#xA0;&#xA0;rename(“images/uploads/thumbs/$element”, “images/uploads/thumbs/thumb_”.$element);
}
?>