CodeIgniter Forums
Show images stored above the root directory - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Show images stored above the root directory (/showthread.php?tid=19452)



Show images stored above the root directory - El Forum - 06-08-2009

[eluser]emily87[/eluser]
In a basic CMS I built without using CI, for security I have been storing uploaded images above the root directory.

To display images I’ve been using:

Code:
echo '< img src="show_image.php?image=’ . $imageFileName . ' " />'

and then in the show image file:
Code:
if (isset($_GET['image'])) {

    // Full image path:
    $image = "full/path/to/uploads/".$_GET['image'];

    // Check image exists and is a file.
    if (file_exists ($image) && (is_file($image))) {
        $name = $_GET['image'];
    } else {
        $image = 'graphics/unavailable.jpg';    
        $name = 'unavailable.jpg';
    }
    
} else { // No image name.
    $image = 'graphics/unavailable.jpg';    
    $name = 'unavailable.jpg';
}

// Get image information.
$ft = getimagesize($image);
$fs = filesize($image);

// Send the file.
header ("Content-Type: {$ft['mime']}\n");
header ("Content-disposition: inline; filename=\"$name\"\n");
header ("Content-Length: $fs\n");
readfile ($image);

I can’t work out how to achieve this in CI.

Can anybody help?


Show images stored above the root directory - El Forum - 06-08-2009

[eluser]mddd[/eluser]
Of course this can be done in CI, and is almost exactly the same.
The only thing is that you get the image name from the CI url in stead of from $_GET.

You could use the following code in your controller called 'Image':
Code:
function _remap()
{
   // get requested image name
   $image = $this->uri->rsegment(2);

   // now you can get the image the same way you did before.
   // so your existing code follows here.
   // ...
}

You'll call the image using a url like www.example.com/image/myImageName.png. That's all.


Show images stored above the root directory - El Forum - 06-08-2009

[eluser]emily87[/eluser]
mddd, so obvious once you pointed it out.

Thank you!