steviecee
07-07-2005, 02:13 PM
Hi, I'm trying to use PHP to stiore and display pictures in a mysql database. At the moment the database seems to store the image without trouble, but i'm having real trouble gettig PHP to display the image in a web page. the code for storing the image is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>testing images</title>
</head>
<body>
<?php
require_once("C:\Server\Apache2\mysql_connect_york.inc");
//check if form has been submitted
if(isset($_POST['submit']))
{
//read uploaded file
$image = addslashes(fread(fopen($_FILES['the_file'] ['tmp_name'], "r"),$_FILES['the_file']['size']));
//generate the query
$query="insert into image_1 values(1,1,'$image','{$_FILES['the_file']['type']}')";
//execute the query and report on success
if(mysql_query($query))
{
echo'Image number'.mysql_insert_id().'has been stored!';
}
else
{
echo 'The image could not be stored in the database!'.mysql_error();
}
//close db connection
//mysql_close();
}
else
{
//display the form
?>
<form action ="store_binary.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Select a file to upload:
<input type="file" name="the_file" />
<br />
<input type="submit" name="submit" value="Submit!" />
</form>
<?php
}
?>
</body>
</html>
and the code to display the image is
<?php
if (isset($_GET['i']))
{
//establish connection to db
require_once("C:\Server\Apache2\mysql_connect_york.inc");
//retrieve image info
$query="select image, from image_1 where image_id={$_GET['i']}";
if($query_result=mysql_query ($query))
{
$image=mysql_fetch_array($query_result);
header("Content-type: $image[1]");
echo $image[0];
}
//close connection
mysql_close();
}
?>
any help would be really appreciated as i've been stuck on this problem for a couple of days now!!
Steve
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>testing images</title>
</head>
<body>
<?php
require_once("C:\Server\Apache2\mysql_connect_york.inc");
//check if form has been submitted
if(isset($_POST['submit']))
{
//read uploaded file
$image = addslashes(fread(fopen($_FILES['the_file'] ['tmp_name'], "r"),$_FILES['the_file']['size']));
//generate the query
$query="insert into image_1 values(1,1,'$image','{$_FILES['the_file']['type']}')";
//execute the query and report on success
if(mysql_query($query))
{
echo'Image number'.mysql_insert_id().'has been stored!';
}
else
{
echo 'The image could not be stored in the database!'.mysql_error();
}
//close db connection
//mysql_close();
}
else
{
//display the form
?>
<form action ="store_binary.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Select a file to upload:
<input type="file" name="the_file" />
<br />
<input type="submit" name="submit" value="Submit!" />
</form>
<?php
}
?>
</body>
</html>
and the code to display the image is
<?php
if (isset($_GET['i']))
{
//establish connection to db
require_once("C:\Server\Apache2\mysql_connect_york.inc");
//retrieve image info
$query="select image, from image_1 where image_id={$_GET['i']}";
if($query_result=mysql_query ($query))
{
$image=mysql_fetch_array($query_result);
header("Content-type: $image[1]");
echo $image[0];
}
//close connection
mysql_close();
}
?>
any help would be really appreciated as i've been stuck on this problem for a couple of days now!!
Steve