How to prevent direct access to my download file using CodeIgniter.
Supposed I stored all of my file in
application/secretdirectory/file1.jpg application/secretdirectory/file2.text application/secretdirectory/file3.zip
usually, I create direct link to access these files.
<a href="application/secretdirectory/file3.zip" > download here <开发者_StackOverflow中文版;/a>
I want to prevent it, I want my link will be
<a href="<? echo site_url() . "download/file3.zip" ?>" > download here </a>
can you help me ???
It's probably a good idea to pass it to a controller like this:
Class File extends Controller{
function download($file){
$data = file_get_contents('path/to/' . $file); // Read the file's contents
$name = $file;
force_download($name, $data);
}
}
Your link would be:
<?php echo anchor('file/download/' . $thefile, 'Download');?>
This way they'd never know which directory it came from.
I'm sure there is a library in CI to assist with this
basically you send headers for the correct MIME type, the content length (filesize()
) and then output to the browser to download echo file_get_contents()
may work)
If using Apache, you'll also want to deny showing directoy content.
<Files .*>
Order Deny,Allow
Deny From All
</Files>
精彩评论