By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,008 Members | 2,962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,008 IT Pros & Developers. It's quick & easy.

How to store images in database by using path and filename

P: 3
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!
Nov 27 '10 #1
Share this Question
Share on Google+
3 Replies


P: 7
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $imgpath = [get from database];
  4. $imgheight = [get from database];
  5. $imgwidth = [get from database];
  6. $imgalt = [get from database];
  7.  
  8. echo "<img src=\"".$imgpath."\" height=\"".$imgheight."\" width=\"".$imgwidth."\" alt=\"".$imgalt."\" />";
  9. ?>
  10.  
  11.  
This would produce (example):
Expand|Select|Wrap|Line Numbers
  1. <img src="img/logo.png" height="300px" width="300px" alt="Company Logo" />
  2.  
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).
Nov 27 '10 #2

P: 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.
Nov 27 '10 #3

P: 7
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)
Nov 27 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.