Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Programming/Scripts

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 5th June 2007, 15:31
edge edge is offline
Moderator
 
Join Date: Dec 2005
Location: The Netherlands
Posts: 2,042
Thanks: 269
Thanked 154 Times in 133 Posts
Default Show image with PHP (error)?

Someone here who might be able to tell me why this does not work.

index.php
PHP Code:
<?php
echo "<hr>\n";
echo 
"<img src=\"images.php?fl=1.jpg\" />\n";
echo 
"<hr>\n";
echo 
"<img src=\"images.php?fl=2.gif\" />\n";
echo 
"<hr>\n";
echo 
"<img src=\"images.php?fl=3.gif\" />\n";
echo 
"<hr>\n";
echo 
"<img src=\"images.php?fl=4.jpg\" />\n";
?>
images.php
PHP Code:
<?php
$filename 
$_REQUEST["fl"];
include 
$filename;
?>
The strange thing is that image 1.jpg, 2.gif and 3.gif showup okay, but 4.jpg does not.

Image 1 = 28 KB
Image 2 = 128 KB
Image 3 = 23 KB
Image 4 = 725 KB

When I try to load image 4 this way: http://domain.tld/images.php?fl=4.jpg I get an error: Parse error: syntax error, unexpected '%' in /var/www/web6/web/4.jpg on line 205

Hmm line 205?? The script does not have than many lines!
(http://domain.tld/4.jpg is working fine)

When I call all the other (smaller) images with http://domain.tld/images.php?fl=... all is working fine!

Anyone here who can enlighten me on this "error"?
__________________
Never execute code written on a Friday or a Monday.
Reply With Quote
Sponsored Links
  #2  
Old 6th June 2007, 17:11
falko falko is offline
Super Moderator
 
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 41,701
Thanks: 1,900
Thanked 2,747 Times in 2,578 Posts
Default

I think PHP tries to interprete 4.jpg as a script. You could try to use file_get_contents() to read the file and then pass it to the browser instead of using include(). Maybe you also need to set headers so that it's clear for the browser that it's an image.
__________________
Falko
--
Download the ISPConfig 3 Manual! | Check out the ISPConfig 3 Billing Module!

FB: http://www.facebook.com/howtoforge

nginx-Webhosting: Timme Hosting | Follow me on:
Reply With Quote
  #3  
Old 9th June 2007, 20:15
sjau sjau is offline
Local Meanie
 
Join Date: Apr 2006
Location: Switzerland
Posts: 1,149
Thanks: 4
Thanked 56 Times in 52 Posts
Default

You have to add a content header according to the image file in the image.php:

e.g.

Code:
header ("Content-type: image/png");
Reply With Quote
  #4  
Old 10th June 2007, 00:51
edge edge is offline
Moderator
 
Join Date: Dec 2005
Location: The Netherlands
Posts: 2,042
Thanks: 269
Thanked 154 Times in 133 Posts
Default

Quote:
Originally Posted by sjau
You have to add a content header according to the image file in the image.php:

e.g.

Code:
header ("Content-type: image/png");
Yes I found this info some days ago, but why does it work (without the "Content-type") for some images, and does it not work for others?
__________________
Never execute code written on a Friday or a Monday.
Reply With Quote
  #5  
Old 10th June 2007, 12:09
Ben Ben is offline
Moderator
 
Join Date: Jul 2006
Posts: 1,029
Thanks: 7
Thanked 62 Times in 56 Posts
Default

Quote:
<?php
$filename = $_REQUEST["fl"];
include $filename;
?>
Very bad code! Pls check where $_REQUEST["fl"] points to and restrict it to onyl one directory or the ones you need.... e.g. with the forgotten header you could easily export /etc/passwd or anything. Maybe safe-mode / open_basedir might help, but that's not reason for implementing such this way

Regarding the last question, maybe the browser interprets the files without header per default to any other imagetype or is able to recognize the appropriate mime-type in some cases.

As falko mentioned I also would make use of file_get_contents e.g. instead of include.
Reply With Quote
  #6  
Old 10th June 2007, 12:18
edge edge is offline
Moderator
 
Join Date: Dec 2005
Location: The Netherlands
Posts: 2,042
Thanks: 269
Thanked 154 Times in 133 Posts
 
Default

Hi Ben,

The code shown, was only as demo. For simplicity I removed all the extra stuff.

I did solve the problem with "Content-type", but I'll have a look at the "file_get_contents" too.

Thank you for your reply.
__________________
Never execute code written on a Friday or a Monday.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache2 Freezes celtic Server Operation 31 28th May 2007 18:18
configuring IPTABLES firewall adityavpratap HOWTO-Related Questions 9 27th May 2006 22:42
Frustrated with ISPConfig install! woozyerdaddee Installation/Configuration 4 19th May 2006 04:38
Installation Fails... :( cyberstorm Installation/Configuration 1 15th January 2006 19:07
Install stop at uuwish, UUDeview SeaWolf Installation/Configuration 6 5th October 2005 00:53


All times are GMT +2. The time now is 00:43.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.