March 16, 201115 yr Just a general question really I was wondering and google isn't spitting out much. If you build a php upload system for pics, videos, files, whatever for different members to upload to a site - example our avatars on this site, AND the files have uniquely generated filenames (hex key etc.) to avoid duplication, is it better to : 1 : Have all files upload to one directory - example "/avatars/". 2 : Have php make a new folder for that member - example "Neil_32/avatars". The one folder system will work, seeing as the filenames are unique and seems a lot easier to code, maintain etc. - but the problem is I'm worried about what happens when you get say 5000 files in one folder, even 10000, 20000 and so on. The seperate folder system - well less files in each folder but you have the added complication of having to create / delete these folders in php - extra coding and what if you have 50000 members = 50000 folders? Up until now I've usually done the one folder method, but suddenly started worrying about expandability - you have 1000 members each with 20 files - 20000 files in one subdirectory for example. Is that going to be a problem?
March 16, 201115 yr Author Just to add an observation in - I notice when doing "image info" on my avatar that this forum seems to use the one folder method. Any opinions?
March 17, 201115 yr I did something like this for a social network i've been working on heres the code /* Insert the users data into our USER_BASE */ $sql = "INSERT INTO user_base(`first_name`,`last_name`,`email`,`password`,`photo`,`website`,`birthday`,`facebook`,`twitter`,`gender`,`side_video`,`activated`,`side_about`,`date_joined`,`time`,`ip`) VALUES('$f_name','$l_name','$email','" . secure($password) . "','$PHOTO','','$birthday','','','$gender','','0','',now(),'" . time() . "','$ip') "; $rel = $mysqli->db_query($sql); /* Create a folder for this member with a unique id number, this folder will hold the members profile photo,photo albums ect.. */ /* Check to see if directory exist if it don't create a user directory */ $id = mysqli_insert_id($mysqli->connect); if(!is_dir(UPLOAD_DIRECTORY."/$id" . "_" . "$f_name" . "_" . "$l_name")){ mkdir(UPLOAD_DIRECTORY."/$id" . "_" . "$f_name" . "_" . "$l_name", 0755); //UPLOAD FILE move_uploaded_file($_FILES['photo']['tmp_name'],UPLOAD_DIRECTORY."/$id" . "_" . "$f_name" . "_" . "$l_name"."/".$_FILES['photo']['name']); }else{ //UPLOAD FILE move_uploaded_file($_FILES['photo']['tmp_name'],UPLOAD_DIRECTORY."/$id" . "_" . "$f_name" . "_" . "$l_name"."/".$_FILES['photo']['name']); } basically u use mkdir to create directories Edited March 17, 201115 yr by webdesigner93
March 17, 201115 yr Author Thanks, webdesigner. You obviously think it's better to use the seperate folder method then? Is there any particular reason for that or is it simply for the sake of a neater directory structure? Or perhaps eay access by say ftp to a particular member's pictures etc.?
March 17, 201115 yr Servers sometimes(?) have limits for the number of files per folder, though if you ran a dedicated server that would be under your control. There are many options for file storage based on users: Assign alphabetical folders for each username, eg /a /b /c etc - you could go even deeper and use the first 2 characters of the name, eg /aa /ab /ac - or even 4/5/6 characters You could assign folders by user #ID
March 17, 201115 yr Author Servers sometimes(?) have limits for the number of files per folder, though if you ran a dedicated server that would be under your control. There are many options for file storage based on users: Assign alphabetical folders for each username, eg /a /b /c etc - you could go even deeper and use the first 2 characters of the name, eg /aa /ab /ac - or even 4/5/6 characters You could assign folders by user #ID Thanks. Yeah that reinforces the way to go is seperate folders. I've in the meantime adjusted my site I'm building to name folders according to WebDesigners naming protocol (although I chose to go forename_surname_id) which seems ok to me. The reason I put the name first is so if I ever have to access a members folder in a hurry I can find it easier by ftp. One thought struck me and I've just built this into mine - I'm keeping a dummy index file in a folder and copying that index file into all the members folders that are being generated when it makes the folders. I just thought maybe that was an idea to stop people just being able to view all the members files by hacking the url... Thanks for the help, lads.
March 18, 201115 yr Thanks, webdesigner. You obviously think it's better to use the seperate folder method then? Is there any particular reason for that or is it simply for the sake of a neater directory structure? Or perhaps eay access by say ftp to a particular member's pictures etc.? The reason i did it is about organization, does not make much since to mix every registers users photos,albums ect.. within one folder when u can create each user there own folder at sign up
March 18, 201115 yr Author The reason i did it is about organization, does not make much since to mix every registers users photos,albums ect.. within one folder when u can create each user there own folder at sign up Cool - +1 to you webdesigner (your first post) and I agree. I have recoded my site last night to accomodate this and it does indeed look more organised. Thanks for your help. And BlueDreamer too.
Create an account or sign in to comment