PHP MYSQL upload & view image

Hello all,
I am looking to some script or a small tutorial that will allow me to upload multiple images to my database using php,
then to able to view them into the browser.
Any help?
LVL 17
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Refer the below code...   -- multiple upload concept...  --- db store and view..

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

First, I wouldn't recommend storing images in your database. It creates bloat, slows it down, makes backup difficult and requires duplication if needed for other purposes. You are much better to upload the images to a directory and just save the link

If you must upload them to your database, there is a PEAR program to help
$abpath = "/home/YOURuserNAME/public_html/images/"; //Absolute path to where images are uploaded.

require 'HTTP/Upload.php';

$upload = new HTTP_Upload('en');       // Language for error messages
$file = $upload->getFiles('userfile'); // return a file object or error
if (PEAR::isError($file)) {    die ($file->getMessage());}// Check if the file is a valid upload
$properties = $file->getProp();
$extension = $file->getProp('ext');
if ($file->isValid()) {    // this method will return the name of the file you moved,    // useful  to save the name in a database    
$file_name = $file->moveTo($abpath);    
if (PEAR::isError($file_name)) {        die ($file_name->getMessage());    }}

$data = addslashes(fread(fopen($abpath.$file_name, "r"), $properties[size]));

Below is the query that I use for uploads, although it probably has a lot more fields of data than you need. Feel free to pare it down.

$file_query="insert into $db.files ( file_name, file_display_name, file_url,  file_file, file_extension, file_size, file_type, file_comments, file_saved_by, file_date_saved, file_start_date, file_end_date, file_status, file_chains, file_show, file_feature) values ('$properties[name]', '$file_display_name', '$abpath', '$data', '$properties[ext]', '$properties[size]', '$properties[type]', '$comments', '$emp', '$date_saved', '$start_date', '$end_date', '$file_status', '$file_chains', '$file_show', '$file_feature') ";
$result_insert_files=mysql_query($file_query, $conn) or die("line 36 ".mysql_error());

To display the images create a PHP program with a name like "show_images.php"
// add your database connection code here.

 $sql = "SELECT file_file, file_type, file_name, file_size FROM files WHERE file_id=$id_files";

  $result = @mysql_query($sql, $conn) or die("line 9 ".mysql_error());
 $data = @mysql_result($result, 0, "file_file");
 $name = @mysql_result($result, 0, "file_name");
 $size = @mysql_result($result, 0, "file_size");
 $type = @mysql_result($result, 0, "file_type");

  header("Content-type: $type");
 header("Content-length: $size");
 header("Content-Disposition: attachment; filename=$name");
 header("Content-Description: PHP Generated Data");
 echo $data;
To view the image
echo" <img src=\"show_images.php?id_files=$image_id\">";
houssam_balloutAuthor Commented:
kumaranmca & :AddyWatson , I had tried your links but they won't work :(
birwin I will try your solution and get back to you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.