[PHP-DEV] Image corruption when retrieving image files from MYSQL database

2002-11-03 Thread Darren McPhee
I have spent the last 3 days trying to figure this out.  And will probably
give up very soon.  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.

HTML
HEADTITLEStore binary data into SQL Database/TITLE/HEAD
BODY

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

if ($submit) {

// connect to the database

require_once('../../Connections/TestServer.php');
mysql_select_db($database_TestServer, $TestServer);

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

$result=MYSQL_QUERY(INSERT INTO master_products
(image_thumbnail,image_thumbnail_name,image_thumbnail_size,image_thumbnail_t
ype) .
VALUES
('$data','$form_data_name','$form_data_size','$form_data_type'));

$id= mysql_insert_id();
print pThis file has the following Database ID: b$id/b;

MYSQL_CLOSE();

} else {

// else show the form to submit new data:
?

form method=post action=?php echo $PHP_SELF; ?
enctype=multipart/form-data
  INPUT TYPE=hidden name=MAX_FILE_SIZE value=100
brFile to upload/store in database:br
input type=file name=form_data  size=40
pinput type=submit name=submit value=submit
/form

?php

}

?

/BODY
/HTML

Here is the code to display the image:-

?php
if($id) {
require_once('../Connections/TestServer.php');
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:
http://192.168.0.11/htdocs/displayimage2.php?id=9  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.

Darren.



CREATE TABLE master_products (
 item_id SMALLINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
 item_code VARCHAR (10) UNIQUE,
 item_dateadded DATETIME DEFAULT '-00-00 00:00:00',
 item_datemodified DATETIME DEFAULT '-00-00 00:00:00',

 category ENUM (none,single herbs,general
vitality,ageing,arthritis,eyesite,prostate,ahlzheimers,
 weight
loss,menopause,depression,fatigue,headaches,insomnia,colds and
flues,allergies,
 healthy heart,cancer prevention,aphrodisiacs,sexual herbs,for
women,for men,books),

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

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

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

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

 image LONGBLOB,
 image_name VARCHAR (50),
 image_size INT UNSIGNED,
 image_type VARCHAR (50),
 image_dateadded DATETIME DEFAULT '-00-00 00:00:00',
 image_datemodified DATETIME DEFAULT '-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 

Re: [PHP-DEV] Image corruption when retrieving image files from MYSQL database

2002-11-03 Thread Wez Furlong
On 04/11/02, Darren McPhee [EMAIL PROTECTED] wrote:
 I have spent the last 3 days trying to figure this out.  And will probably
 give up very soon.  I have written 2 programs (which are very common PHP

Then please spend the extra couple of minutes to read php.net/support.php
and find the correct forum/list to ask this kind of question.

This list is for people developing the core of php or writing extensions
to it, not for people writing php scripts.

--Wez.





-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, visit: http://www.php.net/unsub.php