Renaming files in ascending order w/ PHP. How?

I’m in need of renaming a bunch of images in a directory and am stuck on how to do it. I created a file that I can go to and put in the directory and what I’d like the file name to begin with, but I’d like to rename the files in a numbered fashion in ascending order (i.e., 1.jpg, 2.jpg, 3.jpg, etc.).

I need the images to stay in the appropriate directories so the names have to be different and readable on a unix platform. Right now they’re not. Here is what I have:

<?
if (isset($submit) && $file_name) {

    $directory = $file_directory;
    
    $file_name = stripslashes($file_name);
    $directory = str_replace("\\\\", "/", $directory);
    $directory = stripslashes($directory);
        
         $openfile = opendir($directory);

while($oldfile[] = readdir($openfile)) ;
closedir($openfile);

foreach ($oldfile as $file){

if ($file=="." or $file=="…" or is_dir($file)) continue;

$new_name = $file_name . “-” . $file;

$dir1 = $directory . “/” . $file;
$dir2 = $directory . “/” . $new_name;

rename($dir1, $dir2);

echo $new_name . "- | - ";
echo $dir1;
echo “<br><br>”;
echo $dir2;
echo " | | " . $count . " - " . $numfiles;
echo “<Br><br><br><br>”;

            }
            
            ?&gt;
            &lt;META HTTP-EQUIV="refresh" content="15;URL=index.php"&gt;
            &lt;?
            
            closedir($openfile);

} else {
?>

<form method=“post”>
Directory:<br>
<input type=“text” name=“file_directory” size=“40” style=“border-width: 1px; border-style: solid; font-size: 10px;”><br>
<br>
Name to put in front:<br>
<input type=“text” name=“file_name” size=“40” style=“border-width: 1px; border-style: solid; font-size: 10px;”><br>
<br>
<input type=“submit” name=“submit” value=“Submit” style=“border-width: 1px; border-style: solid; font-size: 10px;”>
</form>
<?
}
?>

Any help would be greatly appreciated. Thanks. :slight_smile:

JP311