although i posted a question looking for suggestions on this already, i went ahead and concocted a scheme to limit the number of files per directory when uploading user images.
i have an event listing project. users can enter an event and upload 3 images for it. rather than just uploading all of the user-submitted images to a single directory, i thought i would try to limit the contents of any given directory to 1000 items or so by creating subdirectories based on the event id. given an event id, you can get the directory for its images by using this function:
define('USER_IMAGE_DIR', '/home/mysite/public/images/');
function event_id_to_dir_path($event_id, $first_call=true) {
if (strlen($event_id) == 0) {
return 'empty';
} elseif (strlen($event_id) < 4) {
return $event_id;
} else {
// the last folder name should always match the event id
$p = substr($event_id, 0, 3) . DIRECTORY_SEPARATOR . event_id_to_dir_path(substr($event_id, 3), false);
if ($first_call) {
return USER_IMAGE_DIR . $p . DIRECTORY_SEPARATOR . $event_id;
} else {
return $p;
}
}
}
advantages:
for a given event id, you can find its location manually by following ticking off the digits of the original event id and following the folders -- or just search for the event id to find the appropriate folder: find -name '999'. for anything under 3 digits, you might get multiple items returned but the correct one should be obvious.
limit of 1000 items per folder means FTP clients or dreamweaver can navigate to proper location if absolutely necessary
each event id gets its own directory, decreasing likelihood of filename collisions.
issues:
I have no idea if this is going to degrade performance in a high traffic environment
Assuming images are generally limited to avg size of 50-100KB, we're talking about 5 to 10 million images on a 500GB disk. Any suggestions for expanding to multiple drives?