Have you tried out mysql function load_file() to upload directly your file
into the database without passing through php variable ?

Like this:

$result=MYSQL_QUERY("INSERT INTO master_products
(image_thumbnail,image_thumbnail_name,image_thumbnail_size,image_thumbnail_t
ype) VALUES

(load_file('$form_data'),'$form_data_name','$form_data_size','$form_data_typ
e')");

If you are not allowed to upload directly $form_data using mysql
load_file(), move first the file $form_data into another allowed directory
with valid filename and try again.

Finally, storing image or binary file into database is not good, put them
into directory instead.

----- Original Message -----
From: "Darren McPhee" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, November 05, 2002 11:24 PM
Subject: [PHP] MYSQL and PHP Storing and retrieving images getting corrupted


> 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>
> <HEAD><TITLE>Store 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 "<p>This 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="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">
>     </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 '0000-00-00 00:00:00',
>  item_datemodified DATETIME DEFAULT '0000-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 '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 LONGBLOB,
>  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 (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



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

Reply via email to