473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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!
Nov 27 '10 #1
3 6270
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
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
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

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

Similar topics

4
by: cover | last post by:
The question is, we have two options to store images, either in a Database (MySQL, Postgres, ...) like blob data, or in the hard disk the file and the path in database. Which option is better?...
1
by: Universal | last post by:
store Images (or any Binary Data) to a SQL Server database using VC++ Can anybody help me in this Thanks in advance
0
by: Big George | last post by:
I'm developing with ASP.NET, VS.NET 2003. Using Crystal Report for VS.NET DataBase: Oracle 10g I store a BLOB field in a table. This field store images, like jpg files. The Stored Procedure...
1
by: nwebhosting | last post by:
Hello and thank you. Could you please help me with this.: i try to store images using the following code, and it just bring a blank screen. i being trying different things and i am not sure if...
0
by: Skiran | last post by:
Hi to everybody, I am handling a large-scale project of MIS (Marketing information System). I am using Vb 6 as front end, Ms Access as back end and Crystal report 8 as reporting tool. 1)...
2
by: dianaj86 | last post by:
Hello All, I have a SQL Server, My question is how can I store images in a database table?? Thanks,
2
by: Syed Mustafa | last post by:
i am a visua basic programmer. i have a problem. i tried to solve it but didnt succeed. we received a credit card details in a text file which is very big. some 100 of records and in between many tab...
2
by: diarehman | last post by:
hi all plz help me. i have to store images in database(access at backend) through vb.net. if u have any information plz reply as soon as possible. thanks
1
by: yuvraj1947 | last post by:
plz help me how to store images and videos in sql database.... thanks in advance..
0
by: kino | last post by:
I am trying to test storing images in a database, I got this code after searching on Google but it doesn't work: protected void Button1_Click(object sender, EventArgs e) { string...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.