View Full Version : Storing Images in DB

06-08-2005, 07:05 AM
Hi, I was wanting to store images inside a database but I have gotten stuck in doing so.

So bacically, i have a

<input type="file" name="pic1">

then, that is suubmitted in a form to another php page. In that, i use

$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?

06-08-2005, 07:15 AM
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.

06-08-2005, 07:47 AM
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">

$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

Aamir. :thumbsup:

06-08-2005, 07:56 AM
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:

<form name="myform" enctype="multipart/form-data" method="post" action="script.php">

$_FILES has the following elements you can then access:

$_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:

$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() (http://uk.php.net/move_uploaded_file).

This section on handling file uploads (http://uk2.php.net/features.file-upload) on php.net is very useful as is the page on error codes (http://uk2.php.net/manual/en/features.file-upload.errors.php).

06-08-2005, 11:30 AM
Amir, You can store images in databases if you want sing the blob column type. must be a bandwidth eater though.

06-08-2005, 11:59 AM
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.

Bill Posters
06-08-2005, 12:10 PM