Welcome Guest, Not a member yet? Register   Sign In
Show images stored above the root directory
#1

[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?
#2

[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.
#3

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

Thank you!




Theme © iAndrew 2016 - Forum software by © MyBB