Jump to content

[SOLVED] Question about renaming all files in a directory


bmw2213

Recommended Posts

Basically, I have a directory of jpgs named IMG_3454.JPG where the 3454 changes for every picture.  There are a lot of files that were deleted so do not exist. 

 

I just downloaded the pictures from a camera and uploaded them onto my server.

 

Now I want to rename them into 1.JPG, 2.JPG, etc. so I can easily make a while loop to display the pictures.

 

My current code is:

 

<?php
$path = "/maggie/";
$dh = opendir($path);
$i=1;
while (($file = readdir($dh)) !== false) {
    if($file != "." && $file != "..") {
$newname = $i.".JPG";
       rename($path."//".$file, $newname);
        $i++;
    }
}
closedir($dh);
?>

 

But it doesn't seem to be working.  Anything I am missing here?

<?php

// Always turn on error reporting when you are debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

$path = "/maggie/";
$dh = opendir($path);
$i = 1;
while (($file = readdir($dh)) !== FALSE)
{
if ($file != "." and $file != "..")
{
	$oldname = "$path/$file";
	$newname = "$path/$i.JPG";
	rename($oldname, $newname);
	$i++;
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.