19,000 thumbnails in 1 folder

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bnashenas1984
    Contributor
    • Sep 2007
    • 257

    19,000 thumbnails in 1 folder

    Hi everyone
    I have a website with 19,000 thumbnails (.jpg format), Not bigger than 5 kb each.

    I was wondering if putting all of them in 1 folder might cause any issue for the server?

    I mean does it slow down the server? If so, what should I do? What does website like bytes.com do with thousands of users (profile pictures)?

    Any information will be appreciated.

    Thanks / Behzad
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    I guess it would depend on the server, or more specifically, the file-system used by the hard drive. On most modern file systems, like ext3, ext4 or NTFS you should be fine up to at least 30k files.

    It is generally a good idea, though, to not store more than a few thousand in the same directory. My rule of thumbs is to make it no more than a thousand images per directory.

    A simple way to have you PHP code do this automatically is to give each image a numeric ID and use that to route the actual file into it's proper directory.
    [code=php]// Insert the image into the db as usual.
    $sql = "INSERT INTO whatever ...";
    $result = mysql_query($sq l);

    // Fetch the ID of the row, and divide by the max number
    // of files allowed per directory to get the number of
    // the directory to place the file in.
    $maxFilesPerDir = 1000;
    $idOfImage = mysql_insert_id ();
    $dirNumber = floor($idOfImag e / $maxFilesPerDir );

    // Determine the path to place the file into.
    $fileDir = "/var/www/images/" . $dirNumber . "/";
    if(!file_exists ($fileDir)) {
    // Creates the dir if it doesn't exist already.
    mkdir($fileDir, 0644);
    }

    // Save the file using it's ID. They are unique by
    // definition, so it makes sure there aren't conflicts.
    $fileLocation = $fileDir . $idOfImage . ".jpeg";
    if(!rename($pat hToOldLocationO fFile, $fileLocation)) {
    die("Failed to move file to it's new location!");
    }

    // NOTE! Use move_uploaded_f ile if these are uploaded
    // files. As you are talking about thumbs, I assume you
    // are generating them yourself.[/code]

    Comment

    • bnashenas1984
      Contributor
      • Sep 2007
      • 257

      #3
      Thanks Atli
      That was very useful.

      Comment

      Working...