kulmu Posted January 16, 2007 Share Posted January 16, 2007 I'm looking for a way to allow members to upload images upon their registration, but also create a directory for their user name to help prevent files of the same name from overwriting each other. Right now the code is:[Code]// Settingsdefine('MAX_FILE_SIZE', 8192); // 8 MBdefine('UPLOAD_PATH', 'images/');$allowed_types = array('image/gif', 'image/pjpeg', 'image/jpeg');// Upload handlingif (isset($_POST['submit'])) { switch ($_FILES['file']['error']) { case UPLOAD_ERR_NO_FILE: $error = 'You did not upload a file!'; break; case UPLOAD_ERR_OK: if ($_FILES['file']['size'] > MAX_FILE_SIZE) { $error = 'File is too large!'; break; } if (!in_array($_FILES['file']['type'], $allowed_types)) { $error = 'This file types is not allowed!'; break; } if (!move_uploaded_file($_FILES['file']['tmp_name'], UPLOAD_PATH . $_FILES['file']['name'])) { $error = 'Could not move file, upload failed!'; break; }[/code]Is there a way to create a directory for each user or would it be easier to just blob it into SQL? How could I accomplish this into my code? mkdir() would work but I am not sure how to tie it in. Quote Link to comment https://forums.phpfreaks.com/topic/34483-mkdir-question/ Share on other sites More sharing options...
JasonLewis Posted January 16, 2007 Share Posted January 16, 2007 why don't you just create there directory when they register an account. Quote Link to comment https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-162428 Share on other sites More sharing options...
HuggieBear Posted January 16, 2007 Share Posted January 16, 2007 I'd create a directory based on their unique UserID when they sign up, then when they login, place their unique id into a session variable and use it when uploading files.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-162444 Share on other sites More sharing options...
kulmu Posted January 17, 2007 Author Share Posted January 17, 2007 The file would be uploaded upon registration if I could do it that way. Basically I want the user to upload their avatar when they create an account. Quote Link to comment https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-162453 Share on other sites More sharing options...
HuggieBear Posted January 17, 2007 Share Posted January 17, 2007 Are they only allowed one avatar?If so then when they register with the site, add their user details to the database and return the unique (auto-incremented) id that gets assigned to them by using [url=http://uk.php.net/manual/en/function.mysql-insert-id.php]mysql_insert_id()[/url].Then maybe use that value to name their avatar. All avatars can be uploaded to the same directory, there's never going to be a clash as no user can have the same id.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-162455 Share on other sites More sharing options...
mattbridges Posted January 17, 2007 Share Posted January 17, 2007 Hi there, I think my response is the same as HuggieBear's (but I've just realised having typed it out!)We had a similar problem with our users uploading images.We solved it by adding a record to an 'image' table. which had the user_id and an auto_increment column.We created directories/5000/10000/15000/20000for exampleThe 5000/10000/15000/20000 relates to the unique user id When for example user id 10020 decides they want to upload a new image, we create a new record in the image table, we then use the id from the image table to create a file in the folder that relates to their user id.so we end up with all of user 10020's files in the 15000 folder and each image has a unique auto incremented id from our mysql image table I hope this makes sense, it's the end of a very long day. It works for us, we've got a ton of images and it's nice and scalable (because you don't end up with too many files in one folder, or too many folders.CheersMatt Quote Link to comment https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-162464 Share on other sites More sharing options...
JasonLewis Posted January 17, 2007 Share Posted January 17, 2007 or if you are going to do multiple avatars then you could simply create a new folder with there username or make it md5 as well for extra protection in an avatar folder. so it would like something like: avatars/users_md5_name/then when they upload images just place them in that folder. simple. Quote Link to comment https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-162683 Share on other sites More sharing options...
kulmu Posted January 22, 2007 Author Share Posted January 22, 2007 had a video card up so didn't have a chance to check this out till today.I've rethought this a bit and think it might be easiest to just try and rename the image on upload using a random generator.[code=php:0]$new_filename = uniqid(rand())[/code]On file upload how do I actually set the new name of the file though? I was trying:[code=php:0]if (!move_uploaded_file($_FILES['file']['tmp_name'], UPLOAD_PATH . $_FILES['file']['$new_filename'])) { $error = 'Could not move file, upload failed!'; break; }[/code]Any help is appreciated. In the end I only want them to have a single image for their avatar. I need to figure out how to assign the new image name to the database as well. I'm a bit of a php newb so I appreciate all the helpI think HuggieBear nailed it though. So I guess the real question is how do I take their image and rename it before it is uploaded to the server. So long as it has a unique id that's all that really matters. Quote Link to comment https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-166771 Share on other sites More sharing options...
mattd8752 Posted January 22, 2007 Share Posted January 22, 2007 Well, if I was using your script, just a warning I could use the name .../whatever to be 2 folders back and in whatever folder and tuduh, I can replace your actual sites images with whatever I want. I had this problem when I did this not to long ago. I changed my folders to use their ID. Quote Link to comment https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-166791 Share on other sites More sharing options...
Jenk Posted January 23, 2007 Share Posted January 23, 2007 random file names are worthless, really really worthless.Just use an incrementing number, at least you can index them that way. Quote Link to comment https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-166882 Share on other sites More sharing options...
kulmu Posted January 23, 2007 Author Share Posted January 23, 2007 [quote author=mattd8752 link=topic=122718.msg510968#msg510968 date=1169507071]Well, if I was using your script, just a warning I could use the name .../whatever to be 2 folders back and in whatever folder and tuduh, I can replace your actual sites images with whatever I want. I had this problem when I did this not to long ago. I changed my folders to use their ID.[/quote]how would I avoid this then?I agree the random idea was a bad one. I want to just rename each image as it is uploaded to their user id. How is this even done though? I can upload the file as I intended right now, but how do I handle the renaming? Quote Link to comment https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-166908 Share on other sites More sharing options...
Jenk Posted January 23, 2007 Share Posted January 23, 2007 http://php.net/move_uploaded_file Quote Link to comment https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-167091 Share on other sites More sharing options...
kulmu Posted January 23, 2007 Author Share Posted January 23, 2007 ok figured out how to rename the file. Problem is that it is not uploading the image, but further into the script it is adding the name into the database:[code=php:0]// Settingsdefine('MAX_FILE_SIZE', 8192); // 8 MBdefine('UPLOAD_PATH', 'images/');$allowed_types = array('image/gif', 'image/pjpeg', 'image/jpeg');$randFilename = uniqid("img_");$newFilename = $randFilename.".".$oldFileExt;// Upload handlingif (isset($_POST['submit'])) { switch ($_FILES['file']['error']) { case UPLOAD_ERR_NO_FILE: $error = 'You did not upload a file!'; break; case UPLOAD_ERR_OK: if ($_FILES['file']['size'] > MAX_FILE_SIZE) { $error = 'File is too large!'; break; } if (!in_array($_FILES['file']['type'], $allowed_types)) { $error = 'This file types is not allowed!'; break; } if (!move_uploaded_file($_FILES['file']['tmp_name'], UPLOAD_PATH . $randFilename.$_FILES['file']['name'])) { $error = 'Could not move file, upload failed!'; break; } }}[/code]For example if I upload a file called uber.jpg the database does show that img_45b645edd59f9uber.jpg is in the database. The problem is the file is not actually uploading to the server.If I try to use the file upload on it's own it seems to work fine. The problem is I want this tied into the user sign up page and force them to provide an image. I'm fairly sure this way will allow users to add an image and not worry about overwriting an existing one. I am probally going to move from rand() to just tagging each image with their user id or name since both would be unique Quote Link to comment https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-167325 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.