SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    PHP Code:
    $targetPath $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
        
    $num 0;
        if (
    $handle opendir($targetPath)) {
        while (
    false !== ($file readdir($handle))) {
            if (
    $file != "." && $file != "..") {
                
    $name substr($file0strrpos($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

  2. #2
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    aaron-fisher.com - PHP articles and more

  3. #3
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, it worked but only for 1.jpg. Now it simple keep overwriting it.

    Have I done something wrong here?

    PHP Code:
    $num 0;
        
    $lastNumber 0;
        if (
    $handle opendir($targetPath)) {
        while (
    false !== ($file readdir($handle))) {
            if (
    $file != "." && $file != "..") {
                
    $name substr($file0strrpos($file'.'));
                
    $num $name;
                if(
    $num $lastNumber 1){
                    
    $num $lastNumber;
                    break;
                }
                
    $lastNumber $name;
            }
        }
        
    closedir($handle);
        }
        
    $next $num+1

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    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.
    PHP Code:
    $filenames = array();
    $lastnumber 0;
    if (
    $handle opendir($targetPath)) {
        for(
    $x 0$x $numfilesinbound$x $x) {
            if (
    $file readdir($handle)) {
               if (
    $file != "." && $file != "..") {
                
    $name substr($file0strrpos($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....

  5. #5
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.:

    PHP Code:
    $num 0;
    $lastNumber 0;
    $directoryList scandir($targetPath);
    foreach(
    $directoryList as $file) {
        if (
    $file != "." && $file != "..") {
            
    $num substr($file0strrpos($file'.'));
            if(
    $num $lastNumber 1){
                
    $num $lastNumber;
                break;
            }
            
    $lastNumber $num;
        }
    }

    $next $num+1
    aaron-fisher.com - PHP articles and more

  6. #6
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks aamonkey it's working great.

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

  7. #7
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  8. #8
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TacMaf View Post
    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
    aaron-fisher.com - PHP articles and more

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by TacMaf View Post
    Thanks aamonkey it's working great.

    Thanks too starmonkey.
    Off Topic:

    **** me, they're mating!

    Eek! an aalion!

  10. #10
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it, thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •