Welcome Guest, Not a member yet? Register   Sign In
How to load files when stored OUTSIDE public folder
#1

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

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

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

[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;
#5

[eluser]Christophe28[/eluser]
Hi,

Waw, thank you for all the support. Wink

Christophe
#6

[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;
#7

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




Theme © iAndrew 2016 - Forum software by © MyBB