Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-08-2005, 06:05 AM   PM User | #1
Hardeep_S
New Coder

 
Join Date: Feb 2005
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Hardeep_S is an unknown quantity at this point
Storing Images in DB

Hi, I was wanting to store images inside a database but I have gotten stuck in doing so.

So bacically, i have a

Code:
<input type="file" name="pic1">
then, that is suubmitted in a form to another php page. In that, i use

Code:
$pic1 = $_POST['pic1'];
$pic1 = addslashes(fread(fopen($pic1, "r")));
However, this method doesn't seem to be working.

Can anyone help me with this method or does anyone know of a good tutorial on exactly how this can be achieved?
Hardeep_S is offline   Reply With Quote
Old 06-08-2005, 06:15 AM   PM User | #2
Serex
Regular Coder

 
Join Date: Mar 2004
Location: Australia
Posts: 217
Thanks: 0
Thanked 1 Time in 1 Post
Serex is an unknown quantity at this point
if you are wanting to store images in a DB then id suggest using a HTML form with the action of uploading the file to your webserver and then storing the image name along with an ID in a DB using MySQL.

if this isnt quite what your trying to do could u please elaborate on the question a little more so we can help you out.
Serex is offline   Reply With Quote
Old 06-08-2005, 06:47 AM   PM User | #3
amir
Regular Coder

 
Join Date: Mar 2005
Location: Pakistan
Posts: 207
Thanks: 0
Thanked 0 Times in 0 Posts
amir is an unknown quantity at this point
we dont store image itself in database, we store path of image in database.
here is the complete working code.

/*****HTML Portion*****/
<form action="urform.php" method="post" enctype="multipart/form-data"> Image <input type="file" size="22" name="image">
<input type="submit" value="Add" name="add">
</form>
/*******************/

PHP Code:
$pimage $_FILES['image']['name'];    
$name $_POST['name'];

copy($_FILES['image']['tmp_name'],"images/$pimage");  //copy image into folder 'images'

$query="INSERT INTO urtable (urfield) values ('$pimage')";  //into database

echo "<img src='images/".$pictures[$i]['productimage']."'height='100' width='125'>";  // to show image 
Now change it according to ur requirements

Regards,
Aamir.
__________________
God helps those who help others.
amir is offline   Reply With Quote
Old 06-08-2005, 06:56 AM   PM User | #4
delinear
Regular Coder

 
Join Date: Feb 2005
Location: West Midlands, UK
Posts: 623
Thanks: 0
Thanked 0 Times in 0 Posts
delinear is an unknown quantity at this point
To access an uploaded file, you use $_FILES['pic1'] rather than $_POST['pic1']. Also, make sure the correct enctype is set in the form or you may have problems uploading:
PHP Code:
<form name="myform" enctype="multipart/form-data" method="post" action="script.php"
$_FILES has the following elements you can then access:
PHP Code:
$_FILES['userfile']['name'// the original name of the file
$_FILES['userfile']['type'// eg image/jpeg
$_FILES['userfile']['size'// filesize
$_FILES['userfile']['tmp_name'// the tmp name of the uploaded file on the server
$_FILES['userfile']['error'// an error code - 0 means no error, 4 means no file uploaded, etc 
I can't test it directly but I think you should be able to do:
PHP Code:
$pic1 addslashes(fread(fopen($_FILES['userfile']['tmp_name'], "r"))); 
Alternatively you may need to move the file to another directory where you an access it, upload it to the database then unlink() the file on the server (you might need to do this if you experience open_basedir errors), you can move the file using move_uploaded_file().

This section on handling file uploads on php.net is very useful as is the page on error codes.
__________________
~ Bazzy
delinear is offline   Reply With Quote
Old 06-08-2005, 10:30 AM   PM User | #5
whackaxe
Senior Coder

 
Join Date: Jun 2002
Location: paris, france
Posts: 1,216
Thanks: 0
Thanked 0 Times in 0 Posts
whackaxe is an unknown quantity at this point
Amir, You can store images in databases if you want sing the blob column type. must be a bandwidth eater though.
__________________
photoshop too expensive? use the GIMP! www.gimp.org
whackaxe is offline   Reply With Quote
Old 06-08-2005, 10:59 AM   PM User | #6
delinear
Regular Coder

 
Join Date: Feb 2005
Location: West Midlands, UK
Posts: 623
Thanks: 0
Thanked 0 Times in 0 Posts
delinear is an unknown quantity at this point
Quote:
Originally Posted by whackaxe
Amir, You can store images in databases if you want sing the blob column type. must be a bandwidth eater though.
If the database is local then it shouldn't really require any extra bandwidth whether the images are on the server or in the database. What it will do is use extra resources to store and retrieve the data from the database but this is likely to be minimal. Even so, the only real reason to do this is to restrict access to files or to aid in creating statistics for file downloads/views. If that's not your aim then it's simpler to just store the images as files on the server.
__________________
~ Bazzy
delinear is offline   Reply With Quote
Old 06-08-2005, 11:10 AM   PM User | #7
Bill Posters
Senior Coder

 
Join Date: Feb 2003
Posts: 1,665
Thanks: 0
Thanked 27 Times in 25 Posts
Bill Posters will become famous soon enough
Fwiw…

http://www.digital-web.com/articles/...th_a_database/
Bill Posters is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:19 PM.


Advertisement
Log in to turn off these ads.