PHP MYSQL upload & view image

Posted on 2010-04-04
Medium Priority
Last Modified: 2013-12-12

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?
Question by:houssam_ballout

Accepted Solution

kumaranmca earned 668 total points
ID: 29749838

Refer the below code...
http://www.phpeasystep.com/phptu/2.html   -- multiple upload concept...
http://www.weberdev.com/get_example-3688.html  --- db store and view..
LVL 12

Assisted Solution

by:Avinash Zala
Avinash Zala earned 668 total points
ID: 29754893

Assisted Solution

birwin earned 664 total points
ID: 29755271

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\">";
LVL 17

Author Comment

ID: 29949812
kumaranmca & :AddyWatson , I had tried your links but they won't work :(
birwin I will try your solution and get back to you

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question