# Renaming files in "for()"

**URL:** <https://forum.kirupa.com/t/renaming-files-in-for/189422>\
**Category:** Uncategorized\
**Created:** [June 2, 2006, 11:31am UTC](https://forum.kirupa.com/t/renaming-files-in-for/189422 "2006-06-02T11:31:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul\_ferrie](https://avatars.discourse-cdn.com/v4/letter/p/919ad9/32.png) [@paul\_ferrie](https://forum.kirupa.com/u/paul_ferrie)\
**Post date:** [June 2, 2006, 11:31am UTC](https://forum.kirupa.com/t/renaming-files-in-for/189422/1 "2006-06-02T11:31:33Z")

</div>

Hi guys.  
i am working on an gallery admin system and have hit a little hurdle.  
Image files on the server are stored named as 1\_thumb.jpg, 1\_main.jpg, 2\_thumb.jpg, 2\_main.jpg and so on…  
my problem is when it comes to deleting some files.  
With image files number 1-8 i delete image set 3 so this leaves in the folder images numbered 1-8 with image set three just been deleted. i am trying to rename the remainder of the files so that they are number 1-7 give that 3 has been deleted.  
At this point the script freeze’s at the point of the that has just been deleted.

```php

$editID = $_POST['editID'];
$fileID = $_POST['fileID'];
$fileDIR = "../media/images/".$editID;
//Delete image
$delThumb = unlink($fileDIR."/".$fileID.'_thumb.jpg');
$delMain = unlink($fileDIR."/".$fileID.'_main.jpg');
//Get new image count
if ($handle = opendir('../media/images/'.$editID.'/')) {
$i=0;
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
    if(substr_count($file, '_main.jpg')==1){
   //
   }else
   if(substr_count($file, '_popup.jpg')==1){
  //
   }else 
   if(substr_count($file, 'spotlite.jpg')==1){
  //
   }else{
           $i++;
   rename($fileDIR ."/". $i . "_thumb.jpg", $fileDIR ."/". $i ."_thumb.jpg");
   rename($fileDIR ."/". $i . "_main.jpg", $fileDIR ."/". $i . "_main.jpg");
         }
  }
    }
}
closedir($handle);
echo "next i =".$i;
//Append file names
/*
for($j=1;$j<$i;$j++){
} 
*/
//
}

```

i feel i should no the answer to this but i just can’t see the wood for the tree’s.  
Any help much appreciatted.
