0

Is it safe to display a image using $_GET for path? For example using this format: image.php?path=/images/example.jpg

morandi3
  • 1,095
  • 3
  • 14
  • 27

3 Answers3

2

Yes you can, just make sure you use isset so that it doesn't throw undefined index if someone fiddles with your URL, also you need to check whether the path is valid else show some other image, like image not found by writing text in alt attribute

if(isset($_GET['index'])) {
   echo '';  
}

Points to be looked for:-

  • Anybody can tinker URL
  • You'll have to sanitize the value
  • Often path's will be changed so be sure you use alt text if image is not found
  • If you don't sanitize, will lead to easy intrusion for hackers

Inshort I suggest you NOT TO DO SO

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
2

Its perfectly safe if you check the path exists after using basename($_GET['path']) on the file name, also define your path to the images folder.

Then check that it is an image with getimagesize($path). If any fail, change the filename to a not found image or such.

<?php 
$path_to_images = '/images/';
$not_found_img  = './path/to/not_found_image.jpg';

// check path is set and not empty
if(empty($_GET['path'])){
    $path = $not_found_img;
}else{
    $path = $path_to_images.basename($_GET['path']);

    // check that image exists
    if(!file_exists($path)){
        $path = $not_found_img;
    }else{
        //Check if image
        if($img_size = getimagesize($path)) {
            //alls good $path validated
        }else{
            $path = $not_found_img;
        }
    }
}

// do somthing with your $path
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0

Completely yes. There are no problems, hackers can't give there bad code, what can hack your page or work with your database. But take care on some other elements.

  • Luckily, [CSRF](http://en.wikipedia.org/wiki/Cross-site_request_forgery) [only](http://stackoverflow.com/questions/2526522/csrf-cross-site-request-forgery-attack-example-and-prevention-in-php) affects visitors :) – thaJeztah May 01 '13 at 07:34