SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Sep 2006
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to create a numbered folder structure?

    I've created an Image upload/process script that saves the images to their own folder (1 folder per upload set). I would like for the folders in this directory to be numbered from 1 to whatever.... like this:

    images/1/
    images/2/
    images/3/
    ... etc

    Right now I'm using a unique id to name the folders and it's too long and not intuitive.

    Is there a way with php to look in the images directory and find out what number needs to come next to name the next folder?

    I hope I've been descriptive enough. Thanks for any guidance or assistance you may be able to provide.

  2. #2
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes there are ways. But I/O operations are expensive and you'd need to perform fair chunk of them. You're better off storing the last ID somewhere (like database).

  3. #3
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by decowski View Post
    Yes there are ways. But I/O operations are expensive and you'd need to perform fair chunk of them. You're better off storing the last ID somewhere (like database).
    It doesn't seem like there would be a need for any expensive operation in this case. This should be enough and perform fast:
    PHP Code:
    $subdirs array_map('intval'scandir("images"));
    $next_dir "images/" . (max($subdirs) + 1); 
    scandir() is pretty fast, it will return also .. and . strings and may return names of other files and directories which are non-numerical, that is why we apply intval() to each of them. If you only have numerically named folders in "images" folder then this will work well and fast.

  4. #4
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    scandir() is pretty fast
    It's not. It is very slow in fact: http://bugs.php.net/bug.php?id=42679

  5. #5
    SitePoint Evangelist
    Join Date
    Sep 2006
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the only folders in this dir will be the numbered folders. There will be no other files..etc.

    So say in the future this dir gets up to 5k numbered folders... would this cause problems?

  6. #6
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rustybuddy View Post
    the only folders in this dir will be the numbered folders. There will be no other files..etc.

    So say in the future this dir gets up to 5k numbered folders... would this cause problems?
    Yes.

  7. #7
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by decowski View Post
    It's not. It is very slow in fact: http://bugs.php.net/bug.php?id=42679
    Wrong. First you should look more closely into this "bug report" before drawing your conclusions. The code presented there does much more than scandir() - it uses is_dir() for each file and also echos data to the browser on each iteration - how can you expect that to be fast?

    Scandir() is very fast, check for yourself first.

    Just for your curiosity, real life results for:

    PHP Code:
    $subdirs array_map('intval'scandir("images"));
    $next_dir "images/" . (max($subdirs) + 1); 
    on a directory with 10 000 numerically named folders:

    PHP 5.2.4, Apache on Windows, old Athlon XP 3000+: 0.033 seconds.

    PHP 5.2.4, Apache on Linux, shared hosting: 0.028 seconds.

    You call it slow??

    Rustybuddy, create thousands of folders on your server and check for yourself, it's better than relying on some obscure bug reports.

  8. #8
    SitePoint Evangelist
    Join Date
    Sep 2006
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    .03 secs.... I believe that's fast enough for me

    Thanks for your guidance!


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
  •