Adding images by number

Hi,

I am trying to create an upload form to add images to a directory. I want the images to be numbered 1.jpg, 2.jpg, etc.

I have had no problems with adding images, with each one becoming the next number. My code is:

$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
	$num = 0;
	if ($handle = opendir($targetPath)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
			$name = substr($file, 0, strrpos($file, '.'));
            $num = $name;
        }
    }
    closedir($handle);
	}
	$next = $num+1;

$targetFile =  str_replace('//','/',$targetPath) . $next . "." . $fileParts['extension'];

The problem I have is if one of the numbers is deleted, how would I replace that image with the next file uploaded?
E.g. if 1.jpg and 3.jpg were deleted from 7 images, how can the next 3 uploads be 1.jpg, 2.jpg and 8.jpg?

Thanks,
Rhys

basic strategy: add another variable $lastNumber. during each pass of the loop check to see if $num - $lastNumber > 1. if it is, break the loop and use that number

Thanks, it worked but only for 1.jpg. Now it simple keep overwriting it.

Have I done something wrong here?


$num = 0;
	$lastNumber = 0;
	if ($handle = opendir($targetPath)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
			$name = substr($file, 0, strrpos($file, '.'));
            $num = $name;
			if($num - $lastNumber > 1){
				$num = $lastNumber;
				break;
			}
			$lastNumber = $name;
        }
    }
    closedir($handle);
	}
	$next = $num+1;

I’m assuming your script already knows how many files are inbound.

Lets try not to crawl this directory more times than we need to.
I’ve stirred this around and spit it out without testing.


$filenames = array();
$lastnumber = 0;
if ($handle = opendir($targetPath)) {
    for($x = 0; $x < $numfilesinbound; $x = $x) {
        if ($file = readdir($handle)) {
           if ($file != "." && $file != "..") {
            $name = substr($file, 0, strrpos($file, '.'));
            $num = $name;
            for($j = 1; $j < $num-$lastNumber;$j++) {
                $filenames[] = $lastNumber+$j;
                $x++;
            }
            $lastNumber = $name;
            }
        } else {
            for($i = 1; $i < ($numfilesinbound - $x); $i++) {
              $filenames[] = $lastNumber + $i;
              $x++;
            }
         }
    }
}

$filenames should now contain an array of filenames to use, in order…

the problem is that readdir() returns the elements as ordered by the filesystem, which I think is usually ordered by date. you can get more control of the sorting order (and write less code) by using scanddir() instead - i.e.:

$num = 0;
$lastNumber = 0;
$directoryList = scandir($targetPath);
foreach($directoryList as $file) {
    if ($file != "." && $file != "..") {
        $num = substr($file, 0, strrpos($file, '.'));
        if($num - $lastNumber > 1){
            $num = $lastNumber;
            break;
        }
        $lastNumber = $num;
    }
}

$next = $num+1; 

Thanks aamonkey it’s working great.

Thanks too starmonkey. I’m not great with arrays though. Probably something I should brush up on.

I found a slight problem. It only works up to 10 as scandir seems to sort alphanumerically? So it would sort 1.jpg, 10.jpg, 2.jpg, etc. Is there an optional parameter to sort numerically?

No, but you can run sort() on the array first

[ot]**** me, they’re mating!

Eek! an aalion![/ot]

Got it, thanks.