Jump to content

rename() using consecutive numbers


tryingtolearn

Recommended Posts

Hi All

 

I am trying to use rename() to take all the images in one directory that have a timestamp and filename

 

and rename them to a new directory but change the names to 1.jpg, 2.jpg, 3.jpg etc...

 

But by using this, it only renames the last one.

 

if ($handle = opendir('./friends')) {
while (false !== ($lv = readdir($handle))) {
        if ($lv != "." && $lv != "..") {	
	$root = getcwd();
	$limit = count(glob('./friends/*.*')); //just * to count subdirs 
    for ($i = 1; $i <= $limit; $i++) {
		copy("./friends/$lv", "$root/friends_no/$i.jpg");
				}
        }
    }
    closedir($handle);
}

 

Anybody have an idea on what I am doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/115542-rename-using-consecutive-numbers/
Share on other sites

Nevermind, I figured it out

 


$ct = "1";
if ($handle = opendir('./friends')) {
while (false !== ($lv = readdir($handle))) {
        if ($lv != "." && $lv != "..") {	
	    $limit = count(glob('./friends/*.*'));
		$up =$ct++;

		$root = getcwd();		
		copy("./friends/$lv", "$root/friends_no/$up.jpg");					
        }
    }
    closedir($handle);
}

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.