CodeIgniter Forums
How to load files when stored OUTSIDE public folder - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: How to load files when stored OUTSIDE public folder (/thread-31542.html)



How to load files when stored OUTSIDE public folder - El Forum - 06-23-2010

[eluser]Christophe28[/eluser]
Hello,

I'm a new user of codeigniter.

I wonder how to load photos when they are stored OUTSIDE the public folder. I used to simply get the filename out the database and load it in the view, something like:
Code:
<img src="http://mydomain.com/uploads/&lt;?php echo $filename; ?&gt;" />
but now I want to store all files outside the public folder in /var/www/mydomain/uploads (instead of /var/www/mydomain/public/uploads) I have been told this is more secure.

So how to tell codeigniter to search for the files outside the public folder and then pass it to the view?

Thx for any help or suggestions!

Christophe


How to load files when stored OUTSIDE public folder - El Forum - 06-23-2010

[eluser]benurv[/eluser]
just make a variable containing the folder eg: $files = /hosting/yourdomain/etc/...
and use it in your view, i always store files outside of my ci root for security reasons

when those files need to be accessed publically, i use a function to pick up the file by id or so and serve it to the browser


How to load files when stored OUTSIDE public folder - El Forum - 06-23-2010

[eluser]Eric Barnes[/eluser]
It is more secure to have your php files outside your web root. However javascript, css, and images all have to be accessible via the web. So you can't move them to a place that isn't accessible.


How to load files when stored OUTSIDE public folder - El Forum - 06-23-2010

[eluser]WanWizard[/eluser]
You could write some code that uses get_file_contents(), something like:
Code:
$details = getimagesize($path . '/' .  $image);
header ('Content-Type: ' . $details['mime']);
echo  file_get_contents($path . '/'  . $image);
exit;



How to load files when stored OUTSIDE public folder - El Forum - 06-24-2010

[eluser]Christophe28[/eluser]
Hi,

Waw, thank you for all the support. Wink

Christophe


How to load files when stored OUTSIDE public folder - El Forum - 06-24-2010

[eluser]Jelmer[/eluser]
I use readfile() for this goal. I'm not entirely sure but I think it's more efficient. readfile() writes the file's contents directly to the output buffer while file_get_contents() first reads it into PHP and then has to write it through PHP. Which I'd guess is less efficient.

The same code using readfile():
Code:
$details = getimagesize($path . '/' .  $image);
header ('Content-Type: ' . $details['mime']);
readfile($path . '/'  . $image);
exit;



How to load files when stored OUTSIDE public folder - El Forum - 06-24-2010

[eluser]WanWizard[/eluser]
You're absolutely right.

I just wrote down the first thing that came to mind, this is a better (and especially with larger images) and a faster solution.