Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts

    PHP image upload script

    This isnt so much a problem, its more of a question, and i would like peoples opinions.

    I would like a php image upload script that only allowes gif and jpeg images under 1mb in size to be uploaded, however once uploaded i would like it to add a username of the person that uploaded it and a user can only upload 1 image, so the real question is, does anyone have any tutorials/can anyone direct me to a good tutorial or a preffered tutorial.

    Thanks.

    Dan
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,369
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    You will need some sort of database to keep track of who uploaded the image.
    So, there would also need to be a user registration portion so they log-in to upload.
    Not sure how much you already have in place?

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    The log in and registration part of my site is fine I just wanted a simple script that will allow a logged in user to upload a single image per user, so that I can later design a photo album type page that will display them all
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #4
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,369
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    Since you have a login system, you must have a MySQL table created
    that contains your user information? Is that correct?

    If so, add a new column to that table called "userPhoto" and then,
    when a user uploads a photo, the photo goes into a directory you have
    for photos and that column will contain the filename of their photo.

    If their column contains a filename, they can't upload one anymore.
    You could allow them to change it though?

  • #5
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    Yep I can do that, however, its more of a photo album of players and they get a prize for uploading a valid picture, therefore there is no need for them to be able to change it.

    also, with the column "userPhoto" should i use varchar? if so, what should be the max size?
    Last edited by Dan13071992; 01-29-2012 at 11:22 AM.
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #6
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,369
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    You'll be saving the filename in your table, not the actual image.
    So, varchar(80) would be fine. The image itself will
    be saved in a directory that you create ... just for
    all of the those images.

    No matter what they upload, you can alter the dimensions
    of it before saving it, and also assign your own filename to it.

    The upload script ... and there are hundreds of them, can
    be a cool AJAX script, or a simple no-frills. Find them via
    Google (PHP file upload script).

  • #7
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts
    Most secure solution: Rename the image to a random string and salt it with the user's username (or use a more complex hashing algorithm) then save the hash in the database with their user id. Now you can call the user id, retrieve their image hash, and generate the URL to display the image in your PHP script. The only other thing is that you'd have to also save the image extension, or you could create a function to find any image with that name, since they'll all be uniquely named. This is the method I use in my database.

    This also saves you space in your DB, since you theoretically only have to store 16-32 characters depending on how long your hashes are.

    The other solution would be to just name the file as their username, so you don't have to store anything in the database. That could lead to predictable file names though, so if someone found the URL, they could just swap in any username and see that picture, not sure if that's a problem though.

  • #8
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    i get what you mean, however the users name will be displayed underneath the image they have uploaded as it will be in the photo album. thanks for all your help guys, im just looking into some scripts now

    Cheers.

    Dan
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •