SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member jneal's Avatar
    Join Date
    Jun 2003
    Location
    vancouver island, Canada
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Upload images using PHP - link primary key to image

    Hello all,
    I am familiar with php and mysql coding. I learned most of my php/mysql coding from the sitepoint book "how to build database driven websites using php and mysql." Great book! Really simplified my learning. However, I have never done any coding with images.

    My dilemma is first trying to figure out if the best way to upload images is to store them in a folder or in the database as a BLOB.

    The next dilemma is how do I add the primary key custnum (auto-incremented) to the images that are uploaded so they can be queried later? Everything been going smoothly until I ran into this image upload issue.

    Thank you in advance.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Generally speaking you store images on the file system, not in databases - but you store its address in your database. (Do some reading on the subject if you like : when should images be stored in a database)

    That address might well become "/images/23.jpg" which is what you seem to be alluding to.

    On successful upload, you would a) move the image from the /tmp directory and b) store it somewhere on your file system.

    All you need to do between those operations is to enter a new row into your table - perhaps with some other descriptive text from the upload operation? the file type (jpg, bng, gif etc) or the date of upload maybe?

    As the new entry is made you ask mysql for the last inserted id number, which you allocate as the file name.

    http://php.net/manual/en/function.mysql-insert-id.php

    Quote Originally Posted by Manual
    int mysql_insert_id ([ resource $link_identifier ] )
    Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).
    One problem you may run into over time is that you cannot tell what an image contains without constantly referring to the table.

    23.jpg

    may be less helpful than:

    fluffy_kitten.jpg

    But generally in situations such as text articles containing an image mean you end up with article id 23 may have a matching image 23 or not.

    You'd have to explain more about your particular situation if you want more detailed discussion.

  3. #3
    SitePoint Member jneal's Avatar
    Join Date
    Jun 2003
    Location
    vancouver island, Canada
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thank you for your response.

    So far, I have the images being stored in the upload_images folder. The pics are being named whatever the user had them named, which is fine. I am just confused as to how I label the pictures the user number that mysql auto generates.
    I would like for the user to enter their info, upload their pictures (up to 12), resize them to 500 pixels wide, then somehow stamp the pictures with the mysql_last_insert_id. I like the method of storing the location in the database. But at some point, I need to be able to search by user number to pull up all the users pictures.

    Currently, the webpage inserts the user info to the user table. Then I pull the unique user number from the user table. I want to figure out how I label the uploaded pics with the last_id from the user table.

    Thank you for your help.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Well, as the image names have to be unique you could retain the image names (you say you are happy with this) then prepend the user id number.

    ie user 23 :

    /images/23_fluffy_kitten.jpg
    /images/23_big_dog.jpg

    Either that or construct a folder for each new user:

    /images/23/fluffy_kitten.jpg
    /images/23/big_dog.jpg

    From your description I am guessing that you create the new user account and store all the images at the same time - is that so?

    If so you create the new users, grab the new users id using last insert id and then store/rename the images accordingly.

    There seems an awful lot to go wrong here though, I'd consider doing it in 2 stages.

    Create new user account, then direct to the upload form and have a hidden field in the upload form containing the user id:
    PHP Code:
    <input type=hidden name=user_id value=23 /> 
    and simply grab $_POST['user_id'] as you do the upload and add it to the path as I described above.

  5. #5
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    One problem you may run into over time is that you cannot tell what an image contains without constantly referring to the table.

    23.jpg

    may be less helpful than:

    fluffy_kitten.jpg

    But generally in situations such as text articles containing an image mean you end up with article id 23 may have a matching image 23 or not.

    You'd have to explain more about your particular situation if you want more detailed discussion.
    This makes perfect sense. Adding a date would also help distinguish images.


Bookmarks

Posting Permissions

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