• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to load files when stored OUTSIDE public folder


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:
<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!


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

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

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


Waw, thank you for all the support. Wink


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():
$details = getimagesize($path . '/' .  $image);
header ('Content-Type: ' . $details['mime']);
readfile($path . '/'  . $image);

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.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.