Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Ways of storing/recording uploaded files (php/mysql)

  • 17-03-2009 3:24pm
    #1
    Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭


    Hi,

    I'm currently working on an application where users can upload and share photos.

    I'm not really sure how i'm going to store the photo's that are uploaded. My current thinking is to upload file with php to users folder the store the relative link to the file in a table in mysql.

    The users folder would be stored on the server and name would be users primary key from users table in database.

    How would I go about naming the files? I've seen other web apps give the files some sort of unique name that looks like a hash..where does this come from as i'm guessing they're unique..?

    Any thoughts on this? or any links..i'm sure this is a common enough operation yet google hasn't been too great on this occasion.

    Thanks..


Comments

  • Closed Accounts Posts: 429 ✭✭Myxomatosis


    EDIT: Actually I re-read your post and I now realize you're not looking on HOW to rename uploaded files but are looking for a scheme to name your uploaded files. D'oh

    [HTML]
    <form action="upload.php" method="POST" enctype="multipart/form-data">
    Choose a file to upload: <input name="file" type="file" />
    <input type="submit" value="Upload File" />
    </form>
    [/HTML]


    upload.php
    [PHP]
    <?php
    /*
    $_FILES["file"]["name"] // the name of the uploaded file
    $_FILES["file"]["type"] // the type of the uploaded file
    $_FILES["file"]["size"] // the size in bytes of the uploaded file
    $_FILES["file"]["tmp_name"] // the name of the temporary copy of the file stored on the server
    $_FILES["file"]["error"] // the error code resulting from the file upload
    */

    $filename = $_FILES["file"]["name"];
    $tmpFile = $_FILES;

    //get extension of the uploaded file.
    $ext = substr($filename, strrpos($filename, '.'));

    //Create a new name for the file
    $newfilename = "somename" . rand(0,1000);

    $target = "upload/" . $newfilename . $ext;

    //Use move_uploaded_file function to move the file from
    //its tmp position to a target and filename of your choice.
    if(move_uploaded_file($tmpFile, $target))
    {
    echo "upload successful";
    }
    else
    {
    echo "upload failed";
    }

    ?>
    [/PHP]


  • Registered Users, Registered Users 2 Posts: 197 ✭✭pauldiv


    techguy wrote: »
    I'm currently working on an application where users can upload and share photos. Thanks..

    Just some food for thought below as it sounds like you have not yet fleshed out exactly what your application will do.

    What is the point of the site? - upload and share could mean several things.
    A list of features might be of help so unless you're building the next iStockPhoto then maybe give some more info.

    If you make usernames unique you could use them for folder naming. Much easier to identify a given username than an auto-generated primary key.
    Store the username in a users table and create a folder with the same name.

    How many pictures are people going to be uploading - 10, 100, 1000?
    How many folders will each user need?

    You need to set limits on the size of uploaded images. It's easy enough to do this before saving each image permanently.

    You could ask the user to provide a title and caption with each image. When the image is uploaded and resized you would rename the image with the title given by the user. Make an image entry in the database with the title and caption then store the image in the user's folder.

    That way you would have meaningful filenames and they would be more searchable.

    If you limit it to one folder per user then a paging system would be required. This way you would not need to limit the amount of images that each user uploads to their folder.

    What about thumbnails? Some galleries have a folder for full sized images and another folder below it for thumbnails. It is more work doing it that way but you get better quality thumbnails as those generated by PHP are not the best.

    The simplest way to do all this would be to:

    Provide one folder per user.
    Rezize the images on upload and store them.
    Generate thumbnails from the main images and use these to display the gallery page.
    Provide a link from each thumbnail back to it's main image.

    My own site uses such a system and if you look you will see that is can be done efficiently and elegantly.

    Bon Chance.


  • Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭techguy


    Myxomatosis, thanks for that but it doesn't really answer my questions.. I was able to do all that all along..

    @pauldiv..
    Just some food for thought below as it sounds like you have not yet fleshed out exactly what your application will do.

    What is the point of the site? - upload and share could mean several things.
    A list of features might be of help so unless you're building the next iStockPhoto then maybe give some more info.

    I have actually thought a lot about this application, along with my partner. We know exactly what it's going to do. It's supposed to be similar to flickr.. each user can have a profile page and upload many photos to albums. Public users can then browse albums if not marked private..thats basically it but there are many more functions also..
    How many pictures are people going to be uploading - 10, 100, 1000?
    How many folders will each user need?

    I haven't really thought about limits yet, there will be a 2MB limit per file as set by PHP and I will leae it at that. I suppose if there is a limit on photos then limiting the number of photos are overall storage is the same.

    There will be a main user folder and a subfolder per each album.
    You need to set limits on the size of uploaded images. It's easy enough to do this before saving each image permanently.

    This is also another concern of mine, how do you go about analysing dimensions and resizing images in php..how do you create your thumbnails?


    I have since setup a quick file upload app that takes a file then stores it in a given directory..and creates a directory (user) if it doesn't already exist.

    Also, I think the idea of a folder for each user is ideal with a sub folder for each album. album and photo filenames will be set by user. What of they give a name that already exists? Just check for that and ask them to enter a different name.

    But what if the user wants to change their username, this is currently allowed. It would mess up the folder structure where using the primary key as folder name would mean user can change name many times..

    Oh and, is the site you talk of the one in your profile, irishlight and colour? It's nice, well done..

    thanks for the insight..


  • Closed Accounts Posts: 429 ✭✭Myxomatosis


    techguy wrote: »
    This is also another concern of mine, how do you go about analysing dimensions and resizing images in php..how do you create your thumbnails?

    Hey hey, I think this time I can help with some code. PHP GD image functions reference here: http://ie2.php.net/gd

    The below creates a medium sized image and a thumbnail from an uploaded gif, jepg or png and saves both files to the hard drive.

    GD generates very good looking thumbnails.

    [PHP]
    $file = $_FILES;
    $type = $_FILES;
    $name = $_FILES;

    switch ($type)
    {
    case "image/gif":
    $image = imagecreatefromgif($file);
    break;
    case "image/jpeg":
    $image = imagecreatefromjpeg($file);
    break;
    case "image/pjepg":
    $image = imagecreatefromjpeg($file);
    break;
    case "image/png":
    $image = imagecreatefrompng($file);
    break;
    }

    //get uploaded images dimensions
    list($width,$height)=getimagesize($file);

    //create image with a width of 300, while maintaining the aspect ratio.
    $newwidth=300;
    $newheight=($height/$width)*$newwidth;
    $tmp=imagecreatetruecolor($newwidth,$newheight);

    imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height);

    $imgfilename = "img/" . rand(100,999) . $prod_id . ".jpg";
    imagejpeg($tmp,$imgfilename,95);

    //create image thumbnail
    $newwidth=75;
    $newheight=($height/$width)*$newwidth;
    $tmp=imagecreatetruecolor($newwidth,$newheight);

    imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height);

    $thumbfilename = "img/". rand(100,999). $prod_id . "_thumb.jpg";
    imagejpeg($tmp,$thumbfilename,95);

    imagedestroy($image);
    imagedestroy($tmp);
    [/PHP]


  • Registered Users, Registered Users 2 Posts: 2,032 ✭✭✭colm_c


    techguy wrote: »
    But what if the user wants to change their username, this is currently allowed. It would mess up the folder structure where using the primary key as folder name would mean user can change name many times..

    The best way to do this is to use an autogenerated ID/hash as the unique identifier for the user and the folder, this separates the user's ID from their photos. You could then use some clever mod_rewrite or other such server filters to present urls of photos to users.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 197 ✭✭pauldiv


    techguy wrote: »
    Oh and, is the site you talk of the one in your profile, irishlight and colour? It's nice, well done..

    Yes Irish Light and Colour is my own site.

    Has your question about the schema been answered yet?

    I assume from what you said you alright with code so I wont talk about it.

    Just some obsevations that you probaly know already about:

    REQUIREMENTS:

    A registered user is allocated a file system folder and any albums they create will exist as subfolders.

    Images are stored in albums rather than in the database.
    (storing images in a database is a waste of time and pretty troublesome)

    At the minimum only the filename and caption of each image needs to be stored in the database for searching and web page output. Other fields could be added later.

    You could use a generic upload form that is stored in a password protected directory.

    After a successful login the user would be re-directed to the upload form with their username stored as SESSION variable.


    UPLOAD SCRIPT:

    There are plenty of checks that you might want to perform before accepting file uploads -

    Accept Only Certain File Types - Check image MIME type. Permit maybe only JPEG, PNG & GIF?

    Prevent files being Overwritten - Concatenate each filename with a Unix date and time stamp. It's simple to do this and will ensure that no 2 images ever have same filename. Ever.

    Remove spaces from filenames - for reasons that should be obvious.

    Define your own max_file_size variable and use this to check if each uploaded file size is larger that max_file_size. If so, reject that upload and send a helpful error message back to the user.

    If all is aok then you resize the image, store it and add a record to the database.

    THUMBNAIL & MAIN IMAGE

    This is where you need to think. All the gallery images on my web site are cropped to either Landscape or Portrait format.

    You should think about the presentation of the main images and then worry about the mechanics of thumbnail generation.

    Assume there are thumbnails on an album page:
    How will they be laid out?
    What happens when a thumnail gets clicked?

    Does a large image open up in the browser window or do you want to show the image within the confines of the web page? If it's the latter then you might need to rezise every image that is uploaded.

    If you you use a javascript like Lightbox to show the main image in a darkened overlay then you dont need to be so careful.

    You could set overall limits on the width and height of all main images to say 800 x 800 pixels.

    Bear in mind that image manipulation takes up a lot of memory. If the site becomes a hit then you want to make the script as lean and efficient as it can be. Needless to say it will all require testing :-(

    All this sounds complicated but it's not if you take the time to do it right.
    =========================================================

    Potential Server Issues -

    Critical Settings:
    (M = Megabytes)
    max_execution_time 30
    max_input_time 60
    post_max_size 8M
    upload_max_filesize 20M

    The above settings are available shared hosting accounts.
    ===========================================================

    Hope that little bit helps.


Advertisement