Image displaying code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
avirup
Forum Newbie
Posts: 12
Joined: Mon Sep 15, 2008 3:07 pm

Image displaying code

Post by avirup »

Hi I have uploaded my image file into mysql database .
Connection is ok.in one file I was trying to display the image I uploaded...in another page using the url ...!!!

That was working fine a day before ..but now instead of image it is showing plenty of peculier chars...!!!
Let's see my code

Code: Select all

<?php require_once("session_con.php");?>
<?php require_once("functions.php");?>
<?php confirm_logged_in();
 
//connect to the database server and the database  
dbconnect();
 
//session data fetching for test
$id= $_SESSION['suid'];
//Get data from the prevoius page
//$id = $_GET['user_id'];
//echo $id;
//Checking the get data is valid or empty
if(is_int($id))
{
echo "No image available";
}else{
//Select image from the database
$sqlf="SELECT  * FROM `user_image` WHERE `user_id` = '".$id."';";
$resultf = mysql_query($sqlf) or die(mysql_error());
 
$row = mysql_fetch_array($resultf) or die(mysql_error());
$fdata = $row['FileData'];
$ftype = $row['MimeType'];
$fname = $row['FileName'];
 
//$fcontent = mysql_result($result,0,"FileData");
//$ftype = mysql_result($result,0,"MimeType");
 
header('Content-type: $ftype');
 
 
echo $fcontent;
 
} 
?>
 
Can anyoneplz help me...!!!
Thank You..in advance
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Image displaying code

Post by onion2k »

There should be NOTHING other than the content type header and the image data sent to the browser. You script is sending 2 carriage returns before the image stuff starts and 1 after. Also you're not populating the $fcontent variable.
avirup
Forum Newbie
Posts: 12
Joined: Mon Sep 15, 2008 3:07 pm

Re: Image displaying code

Post by avirup »

hi....
Can you please tell me how to get rid of that...as I am newbie to php...

plese tell me the change I have to do in the code!!!

Thanks for your reply
avirup
Forum Newbie
Posts: 12
Joined: Mon Sep 15, 2008 3:07 pm

Re: Image displaying code

Post by avirup »

I get it almost ok...but still when running a file instead of showing...
save the file option is coming..
the working code is

Code: Select all

 
[color=#BF0000]<?php require_once("session_con.php");?>
<?php require_once("functions.php");?>
<?php confirm_logged_in();
 
//connect to the database server and the database  
dbconnect();
 
//session data fetching for test
//$id= $_SESSION['suid'];
//Get data from the prevoius page
$id = $_GET['user_id'];
//echo $id;
//Checking the get data is valid or empty
if(is_int($id))
{
echo "No image available";
}else{
//Select image from the database
$sqlf="SELECT  * FROM `user_image` WHERE `user_id` ='".$id."';";
$resultf = mysql_query($sqlf) or die(mysql_error());
 
$row = mysql_fetch_array($resultf) or die(mysql_error());
$fdata = $row['FileData'];
$ftype = $row['MimeType'];
$fname = $row['FileName'];
 
header("Content-Disposition: attachment; filename=$fname");
header('Content-type: $ftype');
echo $fdata;
 
} 
?>
 [/color]
 
 
 
avirup
Forum Newbie
Posts: 12
Joined: Mon Sep 15, 2008 3:07 pm

Re: Image displaying code

Post by avirup »

I saved the file...it's opening with irfan view but not with any other software ...which telling
can not open as the file is unknown type.

Help me please
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Image displaying code

Post by califdon »

What onion2k was telling you is that you can't have lines that send headers

Code: Select all

 header("Content-Disposition: attachment; filename=$fname");
 header('Content-type: $ftype');
in the middle of your script. They must be the very first things that are sent to the browser.

Also it looks to me like you've got your logic backwards in:

Code: Select all

if(is_int($id))
 {
 echo "No image available";
 }else{
 //Select image from the database
 ...
That will echo the message "No image available" IF there is an integer value for $id, which I don't think you intended.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Image displaying code

Post by onion2k »

califdon wrote:What onion2k was telling you is that you can't have lines that send headers

Code: Select all

 header("Content-Disposition: attachment; filename=$fname");
 header('Content-type: $ftype');
in the middle of your script. They must be the very first things that are sent to the browser.
Actually, when you're working with images you typically do have the header() calls right at the end, just before the image data is sent. It's just important that nothing before them sends anything. The very first 5 characters of the script must be <?php and you mustn't break out of PHP at all. The simplest solution is never to put ?> anywhere in a PHP script that generates an image.
avirup
Forum Newbie
Posts: 12
Joined: Mon Sep 15, 2008 3:07 pm

Re: Image displaying code

Post by avirup »

Hi...thanks a lot for answers...but I am afraid i am still unble to show thye image file..

as I wanna show the image file in another page where the code is
imagedisplay.php

Code: Select all

 
<div id="photo"><img src="getimage.php?user_id=<?php echo $_SESSION['suid']; ?>" width="170" height="158" /></div>
 
and my new code is
getimage.php

Code: Select all

 
<?php require_once("session_con.php");
require_once("functions.php"); 
//checking whether user loged in or not
confirm_logged_in();
 
//connect to the database server and the database  
dbconnect();
 
 
$id = $_GET['user_id'];
 
if(!$id)
{
echo "No image available";
}else{
//Select image from the database
$sql="SELECT  * FROM `user_image` WHERE `user_id` ='".$id."';";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
$fdata = $row['FileData'];
$ftype = $row['MimeType'];
$fname = $row['FileName'];
}
//setting the header information to displaythe image
header('Content-type: $ftype');//header info to display the image file
echo $fdata;
 
 
?>
 
 
But my luck it's day 3 and I still unble to display the image in the
imagedisplay.php file

As onion2k told I put the header at the end just before send the image to the broser the result is
a dialog box is coming where it's telling me to download or save the file getimage.php
but when running the display.image.php no image is showing a all.

I still think that my getimage.php is not receiving the userid from the displayimage.php
thats why I was trying to show the image by grabbibg the userid from the session variables..

....what I will do now..??

can anybody correct the code plz...
anyway thanks to those who gave me the idea that it is the problem with the header and nothing else
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Image displaying code

Post by onion2k »

Is the blank line at line 1 in the code you posted actually in the PHP script, or was that just added because you put a carriage return in when you posted the code here? It's enough to break a dynamic image.

I see you still have the closing tag ( ?> ). Delete it. Seriously, it's not needed and it's very likely to cause the script to break the image.

As for checking the user_id value... just comment out the call to header() and the line that echo's the image data and echo the user_id to see what it is.

Other than that, I can only really assume there might be something wrong with the image data in the database if it still isn't working.
avirup
Forum Newbie
Posts: 12
Joined: Mon Sep 15, 2008 3:07 pm

Re: Image displaying code

Post by avirup »

ok...user_id is not coming from the prevous page in the page getimage.php....!!!

as its echoing according to my code that the image is not found....so for now I am using session variable to fetch the id!!!

But will I close the closing tag of PHP??? "?>"

... that's ridiculous as the page is working still..!!!

ok

I have deletd the first blank line!!!

Still I the image is not displaying...!!!
what can be the reason ???
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Image displaying code

Post by onion2k »

avirup wrote:But will I close the closing tag of PHP??? "?>"

... that's ridiculous as the page is working still..!!!
You think it's ridiculous? Ok. You know best. I won't post in this thread any more.
avirup
Forum Newbie
Posts: 12
Joined: Mon Sep 15, 2008 3:07 pm

Re: Image displaying code

Post by avirup »

hi I dont mean it...come on...I dont know the tricks...

I am extremely sorry if u felt bad about that...!!

I am sorry...!!!

What i mean to say is I dont ever know that without using the end php tag the page can run...!!!

Sorry to spoil ur mood....I am really guilty to use that word...!!sorry!!!
Post Reply