Displaying image outside root directory giving error

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

ankuj2004
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Apr 24, 2013 6:41 am

Displaying image outside root directory giving error

Postby ankuj2004 » Wed Apr 24, 2013 6:51 am

Hi ,

I am trying to display some images which are not in my root directory. Lets say the image path is C:\Users\Public\Pictures\SamplePictures and my website is at c:/xampp/htdocs/test.

I am using a php file to do so whose code is

Code: Select all

 <?php
$basedir = '/usr/webfiles';
$image = $_GET['img'];
echo $image;

$fallback = $basedir.'/fallback.gif';

//DETERMINE TYPE
$ext = array_pop(explode ('.', $image));
echo $ext;
$allowed['gif'] = 'image/gif';
$allowed['png'] = 'image/png';
$allowed['jpg'] = 'image/jpeg';
$allowed['jpeg'] = 'image/jpeg';

if(file_exists($image) && $ext != '' && isset($allowed[strToLower($ext)])) {
    $type = $allowed[strToLower($ext)];
   echo "correct";
} else {
    $file = $fallback;
    $type = 'image/gif';
   echo "wrong";
}

header("Content-type: {$type}");
@readfile($file);
exit();
?>


In the main file i am calling it as

Code: Select all

$url = "pictures.php?img=".$url;
echo '<td width="150"><a href=\''.$url.'\' target= "_blank">ScreenShot</a></td>';


where $url is image path ie C:/User/Public/Pictures/SamplePictures/0001.jpg all in front slashes.

But the new tab gives me following error. "The image http://localhost/test/pictures.php?img= C:/User/Public/Pictures/SamplePictures/0001.jpg cannot be displayed because it contains errors."

Any hint about the issue ? I am stuck on it for long time

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Displaying image outside root directory giving error

Postby johnj » Wed Apr 24, 2013 7:03 am

Please read about the difference between a virtual path and physical path. You need to use virtual path, what you are currently using is the physical path of the image.

ankuj2004
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Apr 24, 2013 6:41 am

Re: Displaying image outside root directory giving error

Postby ankuj2004 » Wed Apr 24, 2013 7:13 am

But I am accessing the images which are not in root, so how do I get the virtual path to it ?

ankuj2004
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Apr 24, 2013 6:41 am

Re: Displaying image outside root directory giving error

Postby ankuj2004 » Wed Apr 24, 2013 7:45 am

I changed the php script as

Code: Select all

 <?php
$basedir = 'C:\User\Public\Pictures\\';
$imagedir = $_GET['dir'];
$image = $_GET['img'];

$file = $basedir.$imagedir.'\\'.$image;
$fallback = $basedir.'/fallback.gif';

//DETERMINE TYPE
$ext = array_pop(explode ('.', $file));
$allowed['gif'] = 'image/gif';
$allowed['png'] = 'image/png';
$allowed['jpg'] = 'image/jpeg';
$allowed['jpeg'] = 'image/jpeg';

if(file_exists($file) && $ext != '' && isset($allowed[strToLower($ext)])) {
    $type = $allowed[strToLower($ext)];
   echo "correct";
} else {
    $file = $fallback;
    $type = 'image/gif';
   echo "Wrong";
}
$getInfo = getimagesize($file);
header("Content-type:{$mime}\nContent-Disposition: inline; filename=\"{$image}\"\nContent-length: ".(string)(filesize($file)));
@readfile($file);
exit();
?>


and in the main file

Code: Select all

$url = "pictures.php?dir=".$dir."&img=".$filename;
echo '<td width="150"><a href=\''.$url.'\' target= "_blank">ScreenShot</a></td>';


where

Code: Select all

 $dir = "SamplePictures"
and

Code: Select all

 $img = "0001.jpg"


But the new tab that opens is now printing garbage values as

Code: Select all

‰PNG  IHDRôxÔú pHYs  šœ OiCCPPhotoshop ICC profilexڝSgTSé=÷ÞôBKˆ€”KoR


Any hint now

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Displaying image outside root directory giving error

Postby johnj » Wed Apr 24, 2013 7:31 pm

What is the value for $mime?

ankuj2004
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Apr 24, 2013 6:41 am

Re: Displaying image outside root directory giving error

Postby ankuj2004 » Wed Apr 24, 2013 7:38 pm

Depending on the image type it is either giving image/png or image/jpeg

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Displaying image outside root directory giving error

Postby johnj » Wed Apr 24, 2013 7:47 pm

You should not use @readfile as it will hide whatever error is thrown.

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Displaying image outside root directory giving error

Postby johnj » Wed Apr 24, 2013 7:51 pm

Your header() is in a mess. Please clean it up, read this http://php.net/manual/en/function.header.php

ankuj2004
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Apr 24, 2013 6:41 am

Re: Displaying image outside root directory giving error

Postby ankuj2004 » Wed Apr 24, 2013 8:22 pm

Instead of readfile which function should be used ? Also , I changed my header to

Code: Select all

header("Content-type:".$mime);


Now the following error appears

Code: Select all

"The image “http://localhost/test/pictures.php?dir=SamplePictures&img=0001.png” cannot be displayed because it contains errors."


Return to “PHP coding => General”

Who is online

Users browsing this forum: Baidu [Spider] and 2 guests