Jump to content

Recommended Posts

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]

// Settings
define('MAX_FILE_SIZE', 8192); // 8 MB
define('UPLOAD_PATH',  'images/');
$allowed_types = array('image/gif', 'image/pjpeg', 'image/jpeg');

// Upload handling
if (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.
Link to comment
https://forums.phpfreaks.com/topic/34483-mkdir-question/
Share on other sites

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.

Regards
Huggie
Link to comment
https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-162455
Share on other sites

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
/20000

for example
The 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.

Cheers

Matt
Link to comment
https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-162464
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-162683
Share on other sites

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 help

I 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.
Link to comment
https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-166771
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-166791
Share on other sites

[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?
Link to comment
https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-166908
Share on other sites

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]
// Settings
define('MAX_FILE_SIZE', 8192); // 8 MB
define('UPLOAD_PATH',  'images/');
$allowed_types = array('image/gif', 'image/pjpeg', 'image/jpeg');
$randFilename = uniqid("img_");
$newFilename = $randFilename.".".$oldFileExt;

// Upload handling
if (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
Link to comment
https://forums.phpfreaks.com/topic/34483-mkdir-question/#findComment-167325
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.