Try adding header('Content-length: ' .strlen($data)); header

Darren McPhee wrote:

I have written 2 programs (which are very common PHP
programs) that A) Allows me to upload image files into a MYSQL database
using a simple form. And B) Allows me to retrieve and display the image
using a PHP script. This is my problem:-

I can upload the images ok. I store the binary code, image type, name, and
size within the MYSQL database. On checking the database directly using
DBTools, I can see that the files have uploaded. The file sizes reflect
exactly what was originally on the hard disk of my PC (not sure if this is a
correct gauge).

When I run my PHP program to display the images, maybe only 1 out of 10
actually displays correctly. The rest are broken up or non displayable
images. One image even made a prompt window appear and somehow now causes
Windows paint to load the image instead of the browser. God only knows how
that occurred !!

Below are my (designed by others) 2 programs. The first program is the
upload form. This seems to work ok.

<HEAD><TITLE>Store binary data into SQL Database</TITLE></HEAD>

// code that will be executed if the form has been submitted:

if ($submit) {

// connect to the database

mysql_select_db($database_TestServer, $TestServer);

$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));

$result=MYSQL_QUERY("INSERT INTO master_products
ype) ".

$id= mysql_insert_id();
print "<p>This file has the following Database ID: <b>$id</b>";


} else {

// else show the form to submit new data:

<form method="post" action="<?php echo $PHP_SELF; ?>"
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000">
<br>File to upload/store in database:<br>
<input type="file" name="form_data" size="40">
<p><input type="submit" name="submit" value="submit">





Here is the code to display the image:-

if($id) {
mysql_select_db($database_TestServer, $TestServer);
$query = "select image_thumbnail,image_thumbnail_type from
master_products where item_id=$id";
$result = @MYSQL_QUERY($query);
$data = @MYSQL_RESULT($result,0,"image_thumbnail");
$type = @MYSQL_RESULT($result,0,"image_thumbnail_type");
Header( "Content-type: $type");
echo $data;

I run the above program in the following way: The ID is the item_id
primary key field for whichever record I want to display.

I have tried these programs on a test server here in my room to a test
Apache server and MYSQL test database, and also from my ISP to a MYSQL
database at a server at my ISP. I get exactly the same problem. When I run
the display image program, the images being displayed are always being
displayed the same. Which points the problem towards the upload process
(maybe). If anybody can tell me what the heck is wrong here, I'll give them
a medal !! There is basically some kind of binary corruption going on (it
looks like)

For added information, below is my database table structure. At the moment,
the only part I am actually using relates to the image_thumbnail sections.


CREATE TABLE master_products (
item_code VARCHAR (10) UNIQUE,
item_dateadded DATETIME DEFAULT '0000-00-00 00:00:00',
item_datemodified DATETIME DEFAULT '0000-00-00 00:00:00',

category ENUM ("none","single herbs","general
loss","menopause","depression","fatigue","headaches","insomnia","colds and
"healthy heart","cancer prevention","aphrodisiacs","sexual herbs","for
women","for men","books"),

name VARCHAR (30),
name_dateadded DATETIME DEFAULT '0000-00-00 00:00:00',
name_datemodified DATETIME DEFAULT '0000-00-00 00:00:00',
INDEX idx_name (name),

desc_brief VARCHAR (255),
desc_brief_dateadded DATETIME DEFAULT '0000-00-00 00:00:00',
desc_brief_datemodified DATETIME DEFAULT '0000-00-00 00:00:00',
INDEX idx_desc_brief (desc_brief),

desc_long TEXT,
desc_long_dateadded DATETIME DEFAULT '0000-00-00 00:00:00',
desc_long_datemodified DATETIME DEFAULT '0000-00-00 00:00:00',

price DECIMAL (7,2),
price_dateadded DATETIME DEFAULT '0000-00-00 00:00:00',
price_datemodified DATETIME DEFAULT '0000-00-00 00:00:00',

image_name VARCHAR (50),
image_size INT UNSIGNED,
image_type VARCHAR (50),
image_dateadded DATETIME DEFAULT '0000-00-00 00:00:00',
image_datemodified DATETIME DEFAULT '0000-00-00 00:00:00',

image_thumbnail LONGBLOB,
image_thumbnail_name VARCHAR (50),
image_thumbnail_size INT UNSIGNED,
image_thumbnail_type VARCHAR (50),
image_thumbnail_dateadded DATETIME DEFAULT '0000-00-00 00:00:00',
image_thumbnail_datemodified DATETIME DEFAULT '0000-00-00 00:00:00'

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to