2

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/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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...

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
shweta
  • 21
  • 4
  • Your second example of imagedisplay.php doesn't seem to be different except for the missing opening php tag. – Leif Mar 12 '12 at 13:15
  • It looks like there something missing in your last piece of code, because it doesn't include the query you're talking about. – El Barto Mar 12 '12 at 13:15
  • `$dbh= new DB_Mysql(); $func=new DB_Mysql_code_functions;` - how does the error handling work here, e.g. how can the script determine whether the connection to the database has been established or not? – VolkerK Mar 12 '12 at 13:17
  • problem with connection.tru mysql_eror to find sql error – sandeep Mar 12 '12 at 13:24
  • This is not an answer to your question but are you sure you want to be storing the image data in your db? There are very few scenarios where this actually makes sense. In most situations you should only store the image metadata in the db and keep the image FILES in the FILEsystem. – user1191247 Mar 12 '12 at 13:32
  • the second example of imagedisplay.php had the complete query but was not working. So i removed the query as well as the database connection and tried displaying the image from file folder, it worked. When i added the connection query, it again stopped working, so my guess was there is something wrong with the connection only........Also the error handling is done in the respective classes...it would throw error in case of any problem with connection.... – shweta Mar 12 '12 at 14:34
  • @Andrés Gattinoni-The complete query to fetch a particular image from the database(with image_id=36) in imagedisplay.php cannot be posted in proper format due to the restrictions for the new users :( But it starts showing error in image as soon as it connects to the database. The code without the indentation is in next comment. Please note that i m not using this code since it throws the same error... – shweta Mar 12 '12 at 14:51
  • execute($query); $row=$stmt->fetch_row(); $path="new.jpg"; $handle=fopen($path,'wb'); if(fwrite($handle,$row[3])==FALSE) echo "Cannot write into the file"; else echo "data written successfully"; fclose($handle); $image=imagecreatefromjpeg("new.jpg"); header("Content-type:image/jpeg"); imagejpeg($image); imagedestroy($image); ?> – shweta Mar 12 '12 at 14:51

1 Answers1

1

You have some errors in your queries ("allery", "mage_id").

$query="INSERT INTO allery(image_name,image_type,image,image_size)  VALUES ('".$image_name."','".$image_type."','".$image_data."','".$image_size."')";
...
$query="select * from gallery where mage_id=".$lastimageid;

If you have errors anywhere in your code in imagedisplay.php, they will be output and end up in the image data, corrupting it. See what imagedisplay.php outputs, if you remove imagejpeg() and don't send the image headers. This will give you the necessary information.

Leif
  • 2,143
  • 2
  • 15
  • 26