How to store images in database by using path and filename

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chuck Beck
    New Member
    • Nov 2010
    • 3

    How to store images in database by using path and filename

    I am struggling with this issue right now. I have read in a number of places that you should not store the image itself in the database, but rather only the filepath/filename.

    I can't seem to find anyone who tells how to do that, though. Maybe I just haven't found the right tutorial. Assuming I want to go this route, HOW do I do it?

    What I want to do is give a visitor to my page the option to upload a photo, have the photo file (or its filepath/filename reference) stored in the database, and then display in a different page.

    Where can I find a good tutorial to teach me how to do this. I am pretty new to all this MySQL and PHP stuff, so it must be fairly clearcut.

    Thanks!
    Last edited by Niheel; Nov 30 '10, 05:10 PM. Reason: Your question has been moved. It's much better when you ask questions to ask a new question then in an old question.
  • Curtis Mattoon
    New Member
    • Nov 2010
    • 7

    #2
    If you have the images (i.e. user won't be uploading them), store the path in the database as a string. For example, suppose we have "logo.png" stored in the "img" directory: You'll store the string "img/logo.png" in the database.

    Now, we'll use PHP to generate:
    Code:
    <?php
    
    $imgpath = [get from database];
    $imgheight = [get from database];
    $imgwidth = [get from database];
    $imgalt = [get from database];
    
    echo "<img src=\"".$imgpath."\" height=\"".$imgheight."\" width=\"".$imgwidth."\" alt=\"".$imgalt."\" />";
    ?>
    This would produce (example):
    Code:
    <img src="img/logo.png" height="300px" width="300px" alt="Company Logo" />
    Now, if you're going to have the user upload a photo, you'll need to look up how to upload photos (W3C and PHP documentation both have good examples), and then use the $_FILES array to store the image path in the database (just like above).

    Comment

    • Chuck Beck
      New Member
      • Nov 2010
      • 3

      #3
      Thanks, Curtis. I do need to upload and store images, either in the database or in a directory. The consensus seems to be that storing them in a directory is better, with a pointer in the database. I'm just not sure how to do it. But I think I found a good tutorial.

      I appreciate the info. Thanks again.

      Comment

      • Curtis Mattoon
        New Member
        • Nov 2010
        • 7

        #4
        No problem. Check out the tutorials for uploading files, then use (I believe) the $_FILES['uploadedfile']['path']['name'] variable as the string to store in the database. I'm a little rusty on the file uploads, it's been a while since I've done it, but it's not too bad once you get the actual uploader to work.

        Edit: If I recall correctly, you can specify the upload directory (path). So you'll just have to append $_FILES['uploadedfile']['name'] to $uploadpath. (I think)

        Comment

        Working...