Question : php display image from mysql

I'm trying to create a dynamic webpage with php on mysql database using wampserver to display details of clients with their pictures, I created the recordset and could show the record but the image which is the picture is not displayed instead you have the image name.
Please check my codes, I am a newbie with php

Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:

 
 
 

                                     
                                     
Surname
First name
Family Position
Profession
Profile
   
2009 March

Answer : php display image from mysql

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 do 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" iles=$image_id\">";

Random Solutions  
 
programming4us programming4us