MySql aund PHP
| From: | Damian Fluegel | Date: | Mon, 30 Apr 2001 09:25:45 +0000 |
| Subject: | MySql aund PHP | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-8982@lists.php.net to get a copy of this message | ||
Hello,
I have pictures stored in a database, when I will see them I get only
placeholder of pictures but not the picture self. What is wrong in my code?
Please help me.
Code section:
1) PHP-HTML for storing:
<?
if (!isset($img1)) {
header("Location: http://localhost/php/insert_file.html");
exit;
}
$db = mysql_connect() or die("Can't connect to server.");
mysql_select_db("media", $db) or die("Can't select database.");
$binary_junk = addslashes(fread(fopen($img1, "r"), filesize($img1)));
$insert_data = "INSERT INTO images(img_id, binary_junk, filename, filesize,
filetype) VALUES ('4', '$binary_junk', '$img1_name',
'$img1_size',
'$img1_type')";
mysql_query($insert_data) or die("Couldn't insert data.");
?>
<HTML>
<HEAD>
<TITLE>Successful File Insertion!</TITLE>
<BODY>
<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>
</BODY>
</HTML>
2) Html for viewing:
<HTML>
<HEAD>
<TITLE>My Image</TITLE>
</HEAD>
<BODY>
<H1>Look at this:</H1>
<IMG SRC="show_image2.php">
</BODY>
</HTML>
3) PHP for viewing:
<?
$db = mysql_connect();
$sqlab = "select img_id, filename, binary_junk, filetype from images";
$res = mysql_db_query("media", $sqlab);
$num = mysql_num_rows($res);
if ($num==0)
echo "Leider wurden keine Bilder gefunden<br>";
for($i=0; $i<$num; $i++)
{
$id = mysql_result($res, $i, "img_id");
$fn = mysql_result($res, $i, "filename");
$ft = mysql_result($res, $i, "filetype");
$bj = mysql_result($res, $i, "binary_junk");
header("Content-type: $ft");
echo "$bj" . "<p>";
}
mysql_close($db);
?>
Thanks for your help.
Damian