I am a complete novice and have just begin working with images in PHP and MySQL database. Here is the problem:
The image is being successfully stored in the database table in BLOB field. I can write that in a file(new.jpg) and display the image. Now here is the twsiter - the imagedisplay.php file i created to display the image on the web page can read the new.jpg file from the folder directory. But as soon as i connect to the database to fetch the image data directly from the database (so that i do not have to include new.jpg in the code), it start showing the error "The image contains errors....."
The code used for uploading the image is
<?php
require_once 'login.php'; //contains the classes for connecting to databases
$dbh=new DB_Mysql; //executing queries
$func=new DB_Mysql_code_functions;
session_start();
if(isset($_SESSION['username']))
{
echo<<<_END
<form method="post" action="admin_social_activities.php" enctype="multipart/form-data">
<table width="990">
<tbody>
<tr><td>Select an image file to be uploaded:</td></tr>
<tr><td><input type="submit" value="UPLOAD" /></td></tr>
</tbody>
</table>
</form>
_END;
if(isset($_FILES['imagefile']['tmp_name']))
{
$imagefile=$_FILES['imagefile']['tmp_name'];
$image_size=$_FILES['imagefile']['size'];
$image_name=addslashes($_FILES['imagefile']['name']);
$image_data = addslashes(file_get_contents($imagefile));
$image_array=getimagesize($imagefile);
$image_type=$image_array['mime'];
$image_height=$image_array[1];
$image_width=$image_array[0];
$maxfilesize=2000000;
if($maxfilesize<$image_size)
{
echo "Please upload a smaller image. The size of the image is too large.";
}
else
{
$query="INSERT INTO allery(image_name,image_type,image,image_size) VALUES ('".$image_name."','".$image_type."','".$image_data."','".$image_size."')";
$stmt=$dbh->execute($query);
$lastimageid=mysql_insert_id();
$query="select * from gallery where mage_id=".$lastimageid;
$stmt=$dbh->execute($query);
$row=$stmt->fetch_row();
if(file_exists("new.jpg"))
unlink("new.jpg");
$handle=fopen("new.jpg",'wb');
fwrite($handle,$row[3]);
fclose($handle);
echo "<p>You uploaded this image</p><img src='imagedisplay.php' height=".($image_height/2)." width=".($image_width/2).">";
}
}
}
and the current code for imagedisplay.php file is below and it displays the image fine:
<?php
header("Content-type: image/jpeg");
$image=imagecreatefromjpeg("new.jpg");
imagejpeg($image);
imagedestroy($image);
?>
It stops displaying image as soon as i include the connection query in imagedisplay.php
<?php
require_once 'login.php';
$dbh= new DB_Mysql();
$func=new DB_Mysql_code_functions;
header("Content-type: image/jpeg");
$image=imagecreatefromjpeg("new.jpg");
imagejpeg($image);
imagedestroy($image);
?>
I m stuck on this since days...please help..
OK...so i changed my approach...i m now passing the image_id into the query string and then including imagedisplay.php image tag: Please note that i m not able to write the head part of the code here due to some formating issue. The head section is standard html section pre formatted in Dreamweaver.
<body>
<div class="page shadow-round">
<div id="header">
<div id="logo">
<script type="text/javascript" src="../js/header.js"></script>
</div>
</div>
<div id="menu">
<script type="text/javascript" src="../js/navmenu.js"></script>
<script type="text/javascript">
</script>
</div>
<div class="content overflow" style="height:900px;">
<?php
require_once 'login.php'; //contains the classes for connecting to databases
$dbh=new DB_Mysql; //executing queries
$func=new DB_Mysql_code_functions;
session_start();
if(isset($_SESSION['username']))
{
echo<<<_END
<form method="post" action="admin_social_activities.php" enctype="multipart/form-data">
<table width="990">
<tbody>
<tr><td>Select an image file to be uploaded:</td></tr>
<tr><td><input type="submit" value="UPLOAD" /></td></tr>
</tbody>
</table>
</form>
_END;
if(isset($_FILES['imagefile']['tmp_name']))
{
$imagefile=$_FILES['imagefile']['tmp_name'];
$image_size=$_FILES['imagefile']['size'];
$image_name=addslashes($_FILES['imagefile']['name']);
$image_data = addslashes(file_get_contents($imagefile));
$image_array=getimagesize($imagefile);
$image_type=$image_array['mime'];
$image_height=$image_array[1];
$image_width=$image_array[0];
$maxfilesize=2000000;
if($maxfilesize<$image_size)
{
echo "Please upload a smaller image. The size of the image is too large.";
}
else
{
$query="INSERT INTO gallery(image_name,image_type,image,image_size) VALUES ('".$image_name."','".$image_type."','".$image_data."','".$image_size."')";
$stmt=$dbh->execute($query);
$lastimageid=mysql_insert_id();
echo "<p>You uploaded this image</p>";
echo "<img src='imagedisplay.php?imageid=".$lastimageid."' />";
}
}
}
else
echo "<br/><br/> Your are <span class=\"red\"><b>not Authorized</b></span> to view this page. If you are the Admin, please login with your credentials again. <a href='login_page.php'>Click here to continue</a>";
?>
</div>
</body>
</html>
Now the problem is that the control never goes to imagedisplay.php ie. it fails to reference imagedisplay.php altogether.
the code for imagedisplay.php is below:
<?php
require_once 'login.php';
$dbh= new DB_Mysql();
$func=new DB_Mysql_code_functions;
$id=$_GET['imageid'];
$query="SELECT * FROM gallery where image_id=".$id;
$stmt=$dbh->execute($query);
$row=$stmt->fetch_row();
$imagedata=$row[3];
header("Content-type:image/jpeg");
echo $imagedata;
?>
I have tried all permutation combinations with the quotes, tried echo statements to see if control enters the file....but it does not...it stays in the main file only...i dont understand the reason...please help...