0
votes

How can i display an image on my webpage which i located above webscope? The image is located at /home/image.jpg and the website is located at /home/www/example.com/index.php which refers to http://example.com/.

I have tried this code to retrieve the image, but it does not work :(

    header('Content-type: image/jpeg');
    readfile( realpath( "/home/image.jpg" ));

And I have tried this version too:

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

    $image = imagecreatefromjpeg( realpath('../../image.jpg'));

    // OUTPUT IMAGE AND FREE MEMORY
    imagejpeg($image);  
    imagedestroy($image);  

When I try to read the file with this code:

    fopen( realpath( "../.." ) . '/image.jpg', 'r+' );

It returns this error

    Warning: fopen(/home/image.jpg) [function.fopen]: failed to open stream: No such file or directory in /home/www/example.com/image.php on line 4

Can anyone please help me? I am getting desperate!!!!!

Thanks in advance.

5
did you try ../../image.jpg ? - Haim Evgi
Yes... Does not work either :( - Mathias Bak
get rid of all that garbage - realpath and .. stuff. Why can't you use just plain and simple path as you write it? The error message says there is no such file. Check it's path again. Note that on unix systems filenames are case sensitive. - Your Common Sense
I have deleted realpath. The reason why i used it what because another person told me to use it... - Mathias Bak

5 Answers

1
votes

I'll bet it's a permissions problem: The user your script is running under is likely not to be allowed to access files in /home.

Remove the content-type header, and turn on error_reporting(E_ALL);: PHP will tell you why it can't access the file.

If that is the problem, you'd do better to store the images elsewhere, though, and give that directory the necessary rights.

As a side note, there is no need to create and destroy an image as you do in your example (unless you want this explicitly, e.g. to remove ID3 and other metadata). A simple fpasshtru() will do.

$name = '/path/to/image';
$fp = fopen($name, 'rb');

// send the right headers
header("Content-Type: image/jpeg");
header("Content-Length: " . filesize($name));

// dump the picture and stop the script
fpassthru($fp);
exit;
0
votes

Make sure PHP is allowed to read the file, fopen($filename, 'r+'); will return FALSE if you can't.

0
votes

Your first example is how it should be.
No need to look for anything else.

I have tried this code to retrieve the image, but it does not work :(

When the code doesn't work, a programmer ought to debug it, to find why. And repair.

So, you have to get an error message somehow. Did you look into error_log?

0
votes

Let's add more error handling to your script

<?php
$path = '/home/image.jpg';

$fp = fopen($path, 'rb');
if ( !$fp ) {
  foo($path);
}

if ( !headers_sent() ) {
  header('Content-type: image/jpeg');
  fpassthru($fp);
}

die;
function foo($path) {
  echo 'failed'; flush();

  $out = date("Y-m-d H:i:s\n");
  $out .= " cwd=".getcwd()."\n";
  $out .= " fopen($path) failed\n";
  $dir = dirname($path);
  if ( !is_dir($dir) ) {
    $out .= " $dir is NOT a directory\n";
  }
  else {
    $out .= " $dir contains the following files:\n";
    foreach(glob($dir.'/*') as $fn) {
      $out .= '   '.$fn."\n";
    }
  }
  $out .= "\n\n";

  error_log($out, 3, 'soError.log');
}

Keep an eye on the file soError.log.

0
votes

Perhaps your webhost has open_basedir configured, and the /home directory is outside its limits, making the image unreachable by your script.

You can check if it's set by doing ini_get('open_basedir'); and seeing what's returned.