tags:

views:

85

answers:

2

Hi ....

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 </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 ???

A: 

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>
alex
can you give me a specific snippet ?thanks
adisembiring
+1  A: 

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.

Thorpe Obazee
yeah ..., you are the man. Thanks brur :D
adisembiring