# Image (saved in database) not displaying



## Kan20 (Jul 20, 2009)

Hello:

I read the tutorial for "Uploading files to the database." I could upload the image file to the database but couldn't display it. I think I'm doing something wrong from the getpicture.php section. Here's my getpicture.php code:
<?
if(isset($_GET['fid']))
{
// connect to the database
include "connect.php";

// query the server for the picture
$fid = $_GET['fid'];
$query = "SELECT * FROM images WHERE fid = '$fid'";
$result = mysql_query($query) or die(mysql_error());

// define results into variables
$name=mysql_result($result,0,"name");
$size=mysql_result($result,0,"size");
$type=mysql_result($result,0,"type");
$content=mysql_result($result,0,"content");

// give our picture the proper headers...otherwise our page will be confused
header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
echo $content;

mysql_close();
}else{
die("No file ID given...");
}

?>

And I entered the following code to display the image:
<img src="getpicture.php?fid=1">
But it doesn't display any image, instead it shows a box with a cross. Please help...


~ K


----------



## FredT (Nov 16, 2007)

Visit getpicture.php?fid=1 in your browser as if it were a page and tell us what errors come up.

Also, if you could use *


Code:


[/b] or [b][php][/b] tags when you post code, that would be good.

*


----------



## Kan20 (Jul 20, 2009)

I opened the getpicture.php in my browser, it did not give me any error, in fact it displays a blank page. 

I forgot to mention yesterday, I had to delete some code as I got following error after selecting the image file and clicking on upload button:

Parse error: syntax error, unexpected ';' in C:\wamp\www\imageupload.php on line 125

Which was on this line, so I deleted the following entire code:

```
// if width is over 600 px or height is over 500 px, kill it     
if($width>600 || $height>500) 
{ 
echo $name . "'s dimensions exceed the 600x500 pixel limit."; 
echo ?> <a href="form.html">Click here</a> to try again. <? ; 
die(); 
} 

// if the mime type is anything other than what we specify below, kill it     
if(!( 
$type=='image/jpeg' || 
$type=='image/png' || 
$type=='image/gif' 
)) { 
echo $type .  " is not an acceptable format."; 
echo ?> <a href="form.html">Click here</a> to try again. <? ; 
die(); 
} 

// if the file size is larger than 350 KB, kill it 
if($size>'350000') { 
echo $name . " is over 350KB. Please make it smaller."; 
echo ?> <a href="form.html">Click here</a> to try again. <? ; 
die(); 
}
```

After this it didn't give me any error and file got uploaded in the database. 

Is it because of this?



~ K


----------



## Kan20 (Jul 20, 2009)

Sry, I just tried opening getpicture.php and did not mention the image id. When i opened it by mentioning the image id it shows small box with no image in it.

~ K


----------



## FredT (Nov 16, 2007)

Wait, which line was 125 and what did you take out? I'm a little confused.


----------



## Kan20 (Jul 20, 2009)

The line which gives error is:


```
echo ?> <a href="form.html">Click here</a> to try again. <? ;
```
and the code which contained this line is:

```
// if width is over 600 px or height is over 500 px, kill it     
if($width>600 || $height>500) 
{ 
echo $name . "'s dimensions exceed the 600x500 pixel limit."; 
echo ?> <a href="form.html">Click here</a> to try again. <? ; 
die(); 
}
```
So I deleted the full set of code, but again it gave same error (for that particular line) for this particular code:

```
// if the mime type is anything other than what we specify below, kill it     
if(!( 
$type=='image/jpeg' || 
$type=='image/png' || 
$type=='image/gif' 
)) { 
echo $type .  " is not an acceptable format."; 
echo ?> <a href="form.html">Click here</a> to try again. <? ; 
die(); 
}
```
Same for this code also:

```
// if the file size is larger than 350 KB, kill it 
if($size>'350000') { 
echo $name . " is over 350KB. Please make it smaller."; 
echo ?> <a href="form.html">Click here</a> to try again. <? ; 
die(); 
}
```
So I deleted the all these 3 sections. Then it worked fine.


~ K


----------



## FredT (Nov 16, 2007)

I don't know if you got:
echo ?> <a href="form.html">Click here</a> to try again. <? ;
out of the tutorial, but I'm fairly certain that isn't right.

It's simple, though, all you have to do is take out the echo and the ;

```
// if width is over 600 px or height is over 500 px, kill it     
if($width>600 || $height>500) 
{ 
echo $name . "'s dimensions exceed the 600x500 pixel limit."; 
?> <a href="form.html">Click here</a> to try again. <? 
die(); 
}
```


----------



## Kan20 (Jul 20, 2009)

I removed "echo" and ";" from all the three blocks of code, but then it gave another error pointing to the last line at the closing php tag (?>). The error is:

Parse error: syntax error, unexpected $end in C:\wamp\www\upload_testing\upload.php on line 56

Then I deleted the entire line (from all 3 blocks of code) which says:

```
?> <a href="form.html">Click here</a> to try again. <?
```
Then it worked fine. So I tried displaying the image by pasting the (<img src="getpicture.php?fid=1">) code in the getpicture.php file itself. But it didn't display the image, instead it showed a small box.

Here's the code of getpicture.php file:

```
<html>
<body>
<? 
if(isset($_GET['fid'])) 
{ 
// connect to the database 
include "connect.php"; 

// query the server for the picture 
$fid = $_GET['fid']; 
$query = "SELECT * FROM files WHERE fid = '$fid'"; 
$result  = mysql_query($query) or die(mysql_error()); 

// define results into variables 
$name=mysql_result($result,0,"name"); 
$size=mysql_result($result,0,"size"); 
$type=mysql_result($result,0,"type"); 
$content=mysql_result($result,0,"content"); 

// give our picture the proper headers...otherwise our page will be confused 
header("Content-Disposition: attachment; filename=$name"); 
header("Content-length: $size"); 
header("Content-type: $type"); 
echo $content; 

mysql_close(); 
}else{ 
die("No file ID given..."); 
} 

?> 

<img src="getpicture.php?fid=1">
</body>
</html>
```
Please help me. Okay one question:

Does that three blocks of code (if width is over 600 px or height is over 500 px kill it, if the mime type is anything other than what we specify below kill it and if the file size is larger than 350 KB, kill it) are actually required to display the image? What if we just ignore them right now and see why the image is not displaying.

Because of this my other work is also held up. Thanks for replying to my posts.

~ K


----------



## FredT (Nov 16, 2007)

First off, if your content-disposition is an attachment, then don't include any html in the file. That's wrong.


```
<? 
if(isset($_GET['fid'])) 
{ 
// connect to the database 
include "connect.php"; 

// query the server for the picture 
$fid = $_GET['fid']; 
$query = "SELECT * FROM files WHERE fid = '$fid'"; 
$result  = mysql_query($query) or die(mysql_error()); 

// define results into variables 
$name=mysql_result($result,0,"name"); 
$size=mysql_result($result,0,"size"); 
$type=mysql_result($result,0,"type"); 
$content=mysql_result($result,0,"content"); 

// give our picture the proper headers...otherwise our page will be confused 
header("Content-Disposition: attachment; filename=$name"); 
header("Content-length: $size"); 
header("Content-type: $type"); 
echo $content; 

mysql_close(); 
}else{ 
die("No file ID given..."); 
} 

?>
```
is fine alone for getpicture.php.

This time where I told you to remove the echo and the ; put them back in, but don't close the php block and use single quotes to echo the html... like this:

```
// if width is over 600 px or height is over 500 px, kill it     
if($width>600 || $height>500) 
{ 
echo $name . "'s dimensions exceed the 600x500 pixel limit."; 
echo '<a href="form.html">Click here</a> to try again.'; 
die(); 
}
```
Do that in all three instances you said you were having a problem with.

Hope this helps.


----------



## Kan20 (Jul 20, 2009)

Gr8!ray: Just putting echo and single quotes worked. But image is still not displaying.

I deleted the HTML tags from getpicture.php file. Created new HTML file (just included the necessary code for trial), with below code:

```
<html>

<body>
<img src="getpicture.php?fid=2" />
</body>
</html>
```
Even after this the image is not displaying. What should I do to display the image?


~ K


----------



## FredT (Nov 16, 2007)

I'm trying to scout out what's working and what's not.

Save this as test.php and visit it as test.php?fid=2. Tell me what it says.


```
<? 
if(isset($_GET['fid'])) 
{ 
// connect to the database 
include "connect.php"; 

$fid = $_GET['fid']; 
$query = "SELECT * FROM files WHERE fid = '$fid'"; 
$result  = mysql_query($query) or die(mysql_error());
$numrows = mysql_num_rows($result);
echo 'Results: '.$numrows;

if($numrows>0){
    // define results into variables 
    $name=mysql_result($result,0,"name"); 
    $size=mysql_result($result,0,"size"); 
    $type=mysql_result($result,0,"type"); 
    echo "<br>$name $size $type";
} else {
    echo "<br>No results returned";
}
mysql_close(); 
}else{ 
die("No file ID given..."); 
} 

?>
```


----------



## Kan20 (Jul 20, 2009)

Hello Fred,

I saved the above code as test.php, and opened it as test.php?fid=2 in the browser, but it gives following error:


```
0){ // define results into variables $name=mysql_result($result,0,"name"); $size=mysql_result($result,0,"size"); $type=mysql_result($result,0,"type"); echo "$name $size $type"; } else { echo "
No results returned"; } mysql_close(); }else{ die("No file ID given..."); } ?>
```
What should I do now?

~ K


----------



## FredT (Nov 16, 2007)

That's what it says??? That's not an error... that's part of the file left unparsed.

I've never seen that before...

Add this to the top right below <?


```
error_reporting(E_ALL);
```
and try again.


----------



## Kan20 (Jul 20, 2009)

Fred,

Dat didn't solve d problem. But it got solved after I put "php" before "<?".

Then it displayed the image name, like this:

*Results: 1
Wed_design1.png 156278 image/png*

But it didn't display the image. Then I added the following line of code:

```
$content=mysql_result($result,0,"content");
```
It showed something like this:

```
 ��� IHDR��5��5��� }àS���sRGB�®Îé��� pHYs�����šœ���tIMEÙ3*¢ÆVº�� 
�IDATxÚä¼É®$Y“ßgfgö1"î”™_Vu‹Mª{#@�¡�ž@¢’A$ÔP“ è•è´àª%”Dõ×õ�U™7ï>žÙ´¸%@+íz!ÔÎápÀý¸ÿì¸MÃßýî?§sÎÏO¯Æ¸u��Çã8Ž¯¯¯Ó´h*�3®1Ö4ëºÎËµu‚(5-ëYkSUBJ)cŒ]{È%N—côíÃ}-L$S*ó<£J ZÊ§¯ßŒÒÒÈ¦±Dd›®
ç\Œ1›÷€ìœãJ¹–eYº®3Ú==½X«¯×ëÍÝIJ‰LË²\.!3—u�ïnnÖu*¹´m¨\×J)çyî†VIS _ÞÎÆ çÜù|vFåœ.—IÓ6ãápJ~ûöííííÏþì?“RN—Ùû0c`*Å{/�…@il)e[}Û¹ÝOZkmš33æTRŒRŠÃaØöE iŒÚ÷@D_¾>|Ì¥Xc ³kZ’b
þ|9ÿøãOÆjæ2}Nyšg�ðÞ;×t]BD„u�k)Î5§ÓmÛv)åR 18Û|úôÑhCB‰šËùzAd¼÷û8ono‚÷ß¾=ÎóƒŸ¦É¹æÃÇÏ7§[©µRúîînÇ·ó³–ºïãœ÷>Æ8MsÛ¶Rª T®BP
^)Å\€yßý²ìâîîÞZ‹ˆBæ"•�B�0�×ÊZ¹¾ë„D`öÑK©ŽãñõõüÓ—Ÿ”$””’„,¥"I¥ŒuÖ:fÞ¶]+Ù6m©µ:Bêº~žç}Û–
```
The entire page was filled with this. What's this?

~ k


----------



## FredT (Nov 16, 2007)

Perfect. That page was just a test to debug the code and find the problem, not to display the image.

Add the php to your original file you were working on and that should solve the problem.

Just so you can understand what's going on, it is the 

```
// give our picture the proper headers...otherwise our page will be confused
header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
```
headers that tell the browser it is an image so it doesn't get confused and spit out all that gibberish.


----------



## Kan20 (Jul 20, 2009)

Not quite, it’s still not displaying the image. I don’t know what’s happening. 
Actually, I added php and the header code in my getpicture.php file. Here’s the code of getpicture.php:


```
<?php
if(isset($_GET['fid'])) 
{ 
// connect to the database 
include "connect.php"; 

// query the server for the picture 
$fid = $_GET['fid']; 
$query = "SELECT * FROM files WHERE fid = '$fid'"; 
$result  = mysql_query($query) or die(mysql_error()); 

// define results into variables 
$name=mysql_result($result,0,"name"); 
$size=mysql_result($result,0,"size"); 
$type=mysql_result($result,0,"type"); 
$content=mysql_result($result,0,"content"); 

// give our picture the proper headers...otherwise our page will be confused
header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
echo $content;

mysql_close(); 
}else{ 
die("No file ID given..."); 
} 

?>
```
When I opened the image_test.html file it displayed a small box on the top-right corner. Here’s the code of image_test.html:


```
<html>
</body>
<p>
<img src="getpicture.php?fid=2"></img></p>
</html>
</body>
```
But if I open the getpicture.php?fid=1 directly from the browser, a save as dialog box pops up, and asks me to save the respective image. But it doesn’t display the image.
Please check the above listed codes to see if there’s anything wrong in my code. 


Thanks,
K


----------



## Redcore (Aug 14, 2007)

That is a pretty old tutorial that is less than PHP5 compatible (PHP4 allowed for a lot of bad practices and garbage techniques). I've wanted to update that tutorial but unfortunately I don't have sufficient access to edit it. Typically people IM me and I help them that way.

I just followed the tutorial exactly and minus that ugly HTML echo fix, everything else worked fine. The image embeds into the page (no "Save As..." dialogue) in Firefox, IE, Opera, and Google Chrome. I'm wondering there is some kind of PHP/server setting that isn't displaying the headers as it should. It's hard to say, given that it works fine for me.

That said, the code is ugly and I'm itching to update it.


----------



## FredT (Nov 16, 2007)

What browser are you using, Kan20?


----------



## Kan20 (Jul 20, 2009)

I'm using Firefox 3.5.1.

Thanks,
K


----------



## FredT (Nov 16, 2007)

Well, if you do agree to save it, does the picture come out correct?


----------



## Kan20 (Jul 20, 2009)

No, in fact it doesn't show up anything. The picture manager says no preview.


~ K


----------



## Kan20 (Jul 20, 2009)

No, in fact it doesn't show up anything. Image appears to be blank.


Thanks,
K


----------



## Redcore (Aug 14, 2007)

In phpMyAdmin, browse the table data. "content" should show "[BLOB - XX.X KiB]" - it should be displaying the file size and SHOULD be exact or damn close. Verify the file size matches the file size of the file on your computer. I'm assuming this should be okay since you had all of that garbage output before (which is the image - if you ever opened an image in notepad, that's how it looks) but it's good to check.


----------



## Kan20 (Jul 20, 2009)

Gosh!!!!!!!!!!

At last I got a solution to my problem. Nw I can display the image. Thanks to Fred n Redcore. Special thanks to Fred of course.:laugh:


~ K


----------



## FredT (Nov 16, 2007)

Glad you got it working.


----------



## Redcore (Aug 14, 2007)

Phew. I was running out of ideas.


----------

