4

i had codeigniter project that contain the image folder and i want to make it unreachable from direct url access suppose some one enter url

http://localhost/project/images/Pricelistupdated.pdf

then it will be directly open in browser tab without login so i want to make it disable . when user login and click on download link only that time file will be download and open in new tab . either it can not open when anyone enter direct url in browser i used following

RewriteEngine on 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?projects/project/ [NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?projects/project/.*$ [NC] 
RewriteRule \.(pdf|PDF|jpg|png|gif|PNG)$ - [F]

but this code also disable the download and open in new tab functionality

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kashyap Patel
  • 1,139
  • 1
  • 13
  • 29

3 Answers3

7

If you create a extra folder that can't provide codeigniter framework by default.

Step-1

  • create a .htaccess file in project root directory.

Step-2

save this code in the htaccess file that provide in bellow:

<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine on
RewriteBase /Project_name/
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
</IfModule>

Step-3

  • if already exist htaccess file in root. just add the line of code in your old .htaccess file. as per follow Step-2 example

    Options -Indexes

Now check it.

4

Very Simple! You can try this! Just create an htaccess file with below code:-

Options -Indexes

Keep this file into your image folder. Then Check..

Ibnul Quayum
  • 119
  • 4
  • That stops it generating a list of the files in the directory. It doesn't prevent people from downloading the images if they know the URL. The question is asking about restricting access to logged in users. – Quentin Aug 14 '17 at 17:58
3

You should follow this link How to restrict access to files in a folder in codeigniter

As post described, you need to deny all direct access to your files and read it through controller while user is logged in using authentication.

Community
  • 1
  • 1