I can't help you with the problem described below, but I can tell you
this:

I, like many others, do not store images as BLOBs in a database. I store
them in a regular directory (/images), and just store the image name, path
or URL in the database. This generally creates less hassles.

Mick

On Fri, 7 Dec 2001, phantom wrote:

> With thanks to you who have helped me, I have constructed a PHP script
> that allows the client to upload an image (jpg or gif) that is stored in
> a MySQL Blob field.
> 
> And I have a PHP script that will pull the binary data out of the MySQL
> database and display the upload image for the client.
> 
> THE PROBLEM: HALF MY GIF AND JPG FILES DO NOT DISPLAY PROPERLY WHEN
> VIEWING THE DISPLAY.PHP SCRIPT (see below).  Half display fine, half do
> not.
> 
> Has anyone else had this problem or know what I can do to remedy it?
> Thank you.
> 
> Source code below * form.htm, insert.php, display.php *
> 
> ============ FORM HTM =============
> 
> <HTML>
> <HEAD>
> <TITLE>Insert Binary Data in a Database</TITLE>
> </HEAD>
> 
> <BODY>
> <H1>Upload a File:</H1>
> 
> <FORM enctype="multipart/form-data" method="post" action="insert.php">
> <input type="hidden" name="MAX_FILE_SIZE" value="200000">
> <INPUT type="file" name="img1" size="30">
> <br><br>
> <INPUT type="submit" name="submit" value="Use This File">
> 
> </FORM>
> </BODY>
> </HTML>
> 
> ============ INSERT PHP =============
> 
> <?
> if (!isset($img1)) {
>  header("Location: insert_file.htm?Msg=EmptyUpload");
>  exit();
> }
> 
> $db = mysql_connect("localhost", $DB_Username, $DB_pass)
>  or die ("Unable to connect to server.");
> mysql_select_db($DB_name)
>  or die ("Unable to select DB");
> 
> $binary_junk  = mysql_escape_string(fread(fopen($img1, "r"),
> filesize($img1)));
> // original file used addslashes but here I used mysql_escape_string;
> 
> $insert_data = "INSERT INTO ImageTable (ImgName,ImgSize,ImgType,ImgData)
> VALUES ('$img1_name', '$img1_size', '$img1_type', '$binary_junk')";
> 
> mysql_query($insert_data)
>  or die ("Unable to insert data: ".mysql_error());
> 
> $PKey = mysql_insert_id();
> 
> ?>
> 
> <HTML>
> <HEAD>
> <TITLE>Successful File Insertion!</TITLE>
> 
> <H1>Success!</H1>
> 
> <P>You have inserted the following into your database:<br>
>   <? echo "$img1_name";?>
>   a
>   <? echo "$img1_size";?>
>   byte file with a mime type of
>   <?
>   echo "$img1_type"; ?>.</P>
> View Your Image <a href="display.php?PKey=<?=$PKey?>">HERE</a>
> </BODY>
> </HTML>
> 
> ============ DISPLAY PHP =============
> 
> <?
> $db = mysql_connect("localhost", $DB_Username, $DB_pass)
>  or die ("Unable to connect to server.");
> mysql_select_db($DB_name)
>  or die ("Unable to select DB");
> 
> $get_image = "SELECT ImgType, ImgData FROM ImageTable WHERE PKey=$PKey";
> 
> $get_image_result = mysql_query($get_image)
>  or die ("Unable to get image.");
> 
> $binary_junk = mysql_result($get_image_result,0,ImgData);
> $file_type = mysql_result($get_image_result,0,ImgType);
> 
> header("Content-Type: $file_type");
> header("Content-Length: " . strlen($binary_junk));
> echo "$binary_junk";
> 
> ?>
> 
> 
> 

-- 
################################
Michael Hall            
[EMAIL PROTECTED]
[EMAIL PROTECTED]
http://openlearningcommunity.org



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to