How to store an image in DB and to print it back?
Posted: Thu Jun 06, 2002 8:09 am
I've done this in PHP 4.06 (tested with PHP 4.04)
to put an image into DB i use the following code (assuming a user has apploaded an image via http post)
to accsess an image I use the following html:
to print out an image i use this (image.php):
it works fine for PHP 4.06... but it doesn't work in PHP 4.2...
as i can see every NULL character (0x00) in output is converted to space (0x20)... and a GIF with all nulls converted to spaces is not looking as its author's expecting
Can anybody suggest a solution?
Has anybody come into the same problem?
Can anybody try my code and say me i am not nuts?
to put an image into DB i use the following code (assuming a user has apploaded an image via http post)
Code: Select all
...
$uf = $HTTP_POST_FILESї'img_file'];
$userfile_name = $ufї'name'];
$userfile_type = $ufї'type'];
if ($userfile_type == "" ){
$userfile_type = "application/octet-stream";
}
$userfile_size = $ufї'size'];
$userfile_temp = $ufї'tmp_name'];
if($userfile_temp != "none" && $userfile_size != 0){
$fd = fopen ("$userfile_temp", "rb");
$file = addslashes(fread ($fd, filesize ("$userfile_temp")));
fclose ($fd);
}
else {
$file = "NULL";
}
$query = "INSERT INTO $ntable (id, img, type) VALUES (NULL, "$file","$userfile_type")";
$result = mysql_query ($query)
or print "Query failed.\n";
...Code: Select all
<img src="image.php?id=1">Code: Select all
$id = $HTTP_GET_VARSї"id"];
...
$query = "SELECT img, type FROM $ntable WHERE id="$id"";
$result = mysql_query ($query)
or print "Query failed\n";
$line = mysql_fetch_array($result);
$file = $lineї"img"];
$type = $lineї"type"];
Header("Content-type: $type");
echo $file;as i can see every NULL character (0x00) in output is converted to space (0x20)... and a GIF with all nulls converted to spaces is not looking as its author's expecting