473,385 Members | 1,343 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,385 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 6266
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.