...

View Full Version : displaying pictures from database using php



steviecee
07-07-2005, 03: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

Nightfire
07-07-2005, 03:26 PM
Don't upload the image into the db, it'll use up alot of resources. Simply store the image in a directory and put the filepath into the db. Should also do some security checks before querying the db straight from a variable from the url.

Just link the php page as an image

<img src="displayimage.php?i=1">

steviecee
07-07-2005, 03:32 PM
thanks for that, you're not the first person to advise storing images in a directory. However I'm struggling to find a good tutorial which walks you through the process, do you know of onw?
Steve

Nightfire
07-07-2005, 03:43 PM
Just copy the file and move it to a directory:


copy ($_FILES['imagefile']['tmp_name'], "files/".$_FILES['imagefile']['name']);

That puts it into the 'files' directory

$_FILES['imagefile']['name'] is the name of the file
$_FILES['imagefile']['size'] is the size
$_FILES['imagefile']['type'] is the type, ie image/gif


So in your query to insert into the db, just put the filepath in before the filename and voila



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum