CodeIgniter Forums
storing images outside of public_html - 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: storing images outside of public_html (/showthread.php?tid=16091)

Pages: 1 2


storing images outside of public_html - El Forum - 02-24-2009

[eluser]samseko[/eluser]
My system folder is below the public viewable folder. I have a sub-folder under system called uploads, which is where the uploaded images go. I have their respective direct file path + name in a database, and tagged to a specific member.

Is it possible to then reference this path and any image based on direct path in a view file, as opposed to the conventional way of storing them all in the public_folder and using standard html calls? I need to keep all images out of public viewable area and only allow member to see his / her own images.


storing images outside of public_html - El Forum - 02-24-2009

[eluser]TheFuzzy0ne[/eluser]
You'll probably just need a controller that will work as the front end. The user can request the image via the controller, and the controller can validate them to ensure that they are their images. If it's not, return nothing, or some kind of default warning image.


storing images outside of public_html - El Forum - 02-24-2009

[eluser]samseko[/eluser]
I'm still confused on how i would load the images though -- simple if they are within the public viewable area, but unsure how that would be achieved under the public folder and with a direct path.
Code:
<img src="/home/profile/system/uploads/img.gif">
would not work.


storing images outside of public_html - El Forum - 02-24-2009

[eluser]Jelmer[/eluser]
Use PHP's readfile() function: http://www.php.net/manual/en/function.readfile.php

And don't forget to output the correct MIME type before you use the readfile function.

For example like this:
Code:
// for security reasons, don't allow "../" in the request URI
$server_request = str_replace('../', '', $_SERVER['REQUEST_URI']);
// Rewrite the request to the path on your server
$file = '../outside_webroot/'.$server_request;
// Get the requested path and explode it by the dots
$file_ext = explode('.', $_SERVER['REQUEST_URI']);
// The last value in the array will be the file extention, pop it off
$file_ext = array_pop($file_ext);
// Use the file extention in the MIME type so you don't have to write
// a switch or if/else for every image type (jpg, gif, png)
header('Content-Type: image/'.$file_ext);
readfile($file);



storing images outside of public_html - El Forum - 02-25-2009

[eluser]samseko[/eluser]
I cannot get that code to work -- for some reason it is asking me to download the file as a random 'part' when i try to view the members page with that code.


storing images outside of public_html - El Forum - 02-25-2009

[eluser]TheFuzzy0ne[/eluser]
Please show us your controller.


storing images outside of public_html - El Forum - 02-25-2009

[eluser]samseko[/eluser]
Basic call to the view file from the controller.

The view file has:

Code:
// for security reasons, don't allow "../" in the request URI
$server_request = str_replace('../../system/uploads/', '', $_SERVER['REQUEST_URI']);
// Rewrite the request to the path on your server
$file = '../../system/uploads/feb09-001.jpg';
// Get the requested path and explode it by the dots
$file_ext = explode('.', $_SERVER['REQUEST_URI']);
// The last value in the array will be the file extention, pop it off
$file_ext = array_pop($file_ext);
// Use the file extention in the MIME type so you don't have to write
// a switch or if/else for every image type (jpg, gif, png)
header('Content-Type: image/'.$file_ext);
readfile($file);  ?&gt;



storing images outside of public_html - El Forum - 02-25-2009

[eluser]TheFuzzy0ne[/eluser]
It should be a controller, not a view.

./system/application/libraries/MY_Controller.php
Code:
&lt;?php

class MY_Controller extends Controller {

    function MY_Controller()
    {
        parent::Controller();
    }
    
    function user_images($image_name="")
    {
        if ($image_name)
        {
            // Some validation here.
            
            // If the requested image belongs to the user requesting it...
            
            // for security reasons, don't allow "../" in the request URI
            
            $server_request = str_replace('../../system/uploads/', '', $_SERVER['REQUEST_URI']);
            // Rewrite the request to the path on your server
            $file = '../../system/uploads/feb09-001.jpg';
            // Get the requested path and explode it by the dots
            $file_ext = explode('.', $_SERVER['REQUEST_URI']);
            // The last value in the array will be the file extention, pop it off
            $file_ext = array_pop($file_ext);
            // Use the file extention in the MIME type so you don't have to write
            // a switch or if/else for every image type (jpg, gif, png)
            header('Content-Type: image/'.$file_ext);
            readfile($file);  ?&gt;
        }
    }
}

// End of file: MY_Controller.php
// Location: ./system/application/libraries/MY_Controller.php
The above code is untested.

You could then use something like this from within your view.
Code:
<img src="/somecontroller/user_images/someimage.jpg" />

I haven't done it all for you... This is just an example to hopefully demonstrate one possible method to achieve the results you're after. There are probably many other ways to achieve the same thing. Without knowing your application structure, it's hard to comment on which method will be best for you.

Hope this helps.


storing images outside of public_html - El Forum - 02-25-2009

[eluser]Jelmer[/eluser]
There's more wrong with the code.

The following does nothing usefull:
Code:
$server_request = str_replace('../../system/uploads/', '', $_SERVER['REQUEST_URI']);
That statement used to read:
Code:
str_replace('../', '', $_SERVER['REQUEST_URI']);
Because it is meant to replace any previous directory requests (../) by the user to prevent them from requesting anything they like. I presume that '../../system/uploads/' in general won't be in the request URI, so replacing it is kinda useless.

Code:
$file = '../../system/uploads/feb09-001.jpg';
The fixed file is for testing I presume?


storing images outside of public_html - El Forum - 02-25-2009

[eluser]samseko[/eluser]
TheFuzzy0ne, Jelmer:

it works!! I used the combination of both your previous posts. The image file was static as i was testing to see if it would work before i polled the database.

Thanks heaps guys!!