LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 12-07-2006, 04:45 AM   #1
jshannon
LQ Newbie
 
Registered: Dec 2006
Distribution: SuSE, Fedora
Posts: 2

Rep: Reputation: 0
Question Apache-php problem on fedora


I cannot get apache on fedora 5 to display jpeg image from a simple script that runs fine on suse 10.1.

<?php
//picture-test.php
// get contents of a file into a string
$filename = "trafficamber.jpg";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

header("Content-type: image/jpeg");

echo $contents;

?>


On Suse 10.1 this displays the image. On Fedora 5 the url is displayed. The image is less than 1k in size and I can display it from the fedora server by pointing my browser directly at it.

I have looked through the apache and php conf files but can't see anything obvious.

Help, What am I missing??

Thanks
 
Old 12-08-2006, 11:02 AM   #2
johnewing
Member
 
Registered: Jul 2003
Location: Scotland
Posts: 34

Rep: Reputation: 16
That is a wierd problem are you sure that apache has permission to read the file and that you have the filename correct?

Try something like this to check

<?php
//picture-test.php
// get contents of a file into a string
$filename = "trafficamber.jpg";

echo "<br>file exists:".file_exists($file_name);
$handle = fopen($filename, "r");
echo "<br>handle:$handle<br>";

$contents = fread($handle, filesize($filename));
fclose($handle);

// header("Content-type: image/jpeg");

echo $contents;

?>

If the file exists and handle is valid then it might be a configuration problem

J.
 
Old 12-08-2006, 03:25 PM   #3
Boby
Member
 
Registered: Feb 2004
Location: Sighisoara/Cluj-Napoca (Romania)
Distribution: CentOS 4, Fedora Core 6
Posts: 781

Rep: Reputation: 31
Thumbs up Try this method

You have to make sure no output is made before the image headers are sent, not even one character or it will fail.
I'm not exactly sure when or why the "headers_sent" function is not working correct, but usually if you see the filename in the output something was wrong before the images was rendered in the browser.

Here is a simplified version of a script that I've used on an application, I don't advise you to use the "exit" messages, but instead maybe just trigger an error message and continue with the next step of the script.
The following example is just to make you an ideea on how to address this problem. It's checking a lot of things before the image is drawn, and it uses the GD + libJPEG libraries to display the image. I suggest this over you method.

PHP Code:
<?php
//Do not show warnings and notices
error_reporting (E_ALL E_WARNING E_NOTICE);

//Remove these 2 lines when going online
error_reporting (E_ALL); //Debug
ini_set ('display_errors'true); //Debug

$imageFilename 'test.jpg';

//Check if GD library is loaded
if (!extension_loaded ('gd'))
{
   exit (
'No GD library available!');
}

$gdInfo gd_info();

//Check for JPEG support
if (!$gdInfo['JPG Support'])
{
   exit (
'No JPEG library available! <code>libjpeg</code>');
}

//Check if headers were allready sent
//Doesn't make any sens to continue if they were sent
if (!headers_sent())
{
   
//Check if file exists
   
if (!file_exists ($imageFilename))
   {
      exit (
'File does not exist!');
   }

   
//Check if file is readable
   
if (!is_readable ($imageFilename))
   {
      exit (
'File is not readable, please change permissions!');
   }

   
//Attempt to open image
   
$image = @ imagecreatefromjpeg ($imageFilename);

   if (!
$image)
   {
      
//Could not load image
      
exit ('Error loading image!');
   }

   
//Send headers
   
header ('Content-transfer-encoding: binary');
   
header ('Content-Type: image/jpeg');

   
//Draw image
   
imagejpeg ($image);

   
//Destroy image
   
imagedestroy ($image);
}
else
{
   
//Do not make any other output to the browser
   //before image is rendered
   
die ('Headers were allready sent!');
}
?>
Make sure you remove these lines when going online with the script, they are ment to be there just for debugging:
PHP Code:
//Remove these 2 lines when going online
error_reporting (E_ALL); //Debug
ini_set ('display_errors'true); //Debug 
Hope it helps!
Boby
 
Old 12-11-2006, 05:57 AM   #4
jshannon
LQ Newbie
 
Registered: Dec 2006
Distribution: SuSE, Fedora
Posts: 2

Original Poster
Rep: Reputation: 0
Smile Thanks

Thanks for the help.

As I said this was just a test script. The actual app has all the checking in place and is pulling image, pdf, mp3 and video files from a database.

The thing that did the job was the line..

header ('Content-transfer-encoding: binary');


As soon as I added this line it worked.

Jock
 
  


Reply

Tags
apache2, fedora 5, php5


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Installing PHP 4 on Apache 2.2 on Fedora 5 lovalixir Linux - Software 0 08-07-2006 02:37 AM
Fedora Core 5 and PHP/Apache johnejo Fedora 0 06-12-2006 10:38 AM
Fedora Core 3 And PHP + Apache xbigs Fedora 1 12-17-2005 08:24 AM
Fedora Apache and PHP RJDavison Fedora 1 05-22-2004 01:11 AM
Apache php index.php problem neurotic Linux - Software 3 11-18-2003 07:02 PM


All times are GMT -5. The time now is 11:41 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration