如何限制访问文件的文件夹在codeigniter [英] How to restrict access to files in a folder in codeigniter
问题描述
你好,我已经使用codeigniter来开发自己的网站,但是当搜索在谷歌自己的网站,谷歌展示了链接到文件(PDF)在一个特定的文件夹中,用户可以直接查看这些文件(PDF)无登录。我想限制谷歌直接显示到这些文件的链接。 例如:www.mywebsite / MyFolder中/ MYFILE
Hello I have used codeigniter to develop my website but when my website is searched in google , google shows up links to the files (pdf) in a particular folder and user can view these files (pdf) directly without login. I want to restrict google to directly show the links to these files. ex: www.mywebsite/myfolder/myfile
请指引我,我怎么能做到这一点。谢谢你。
Please guide me how can I accomplish this. Thank you.
推荐答案
从$ P $稍加修改pvious <一href="http://stackoverflow.com/questions/12247040/how-to-restrict-access-to-files-in-a-folder-in-$c$cigniter/12247430#12247430">answer :
A little modification from the previous answer :
与内容放置在的.htaccess文件夹
Place the .htaccess in your file folder with the content
order deny, allow
deny from all
这将拒绝所有访问该特定文件夹。
This will deny all access to that particular folder.
有关访问文件的控制器体写一个函数 -
For accessing the files write a function in controller with body -
public function index($file_name){
if($this->login()) // login check
if (preg_match('^[A-Za-z0-9]{2,32}+[.]{1}[A-Za-z]{3,4}$^', $file_name)) // validation
{
$file = 'my_path/'.$file_name;
if (file_exists($file)) // check the file is existing
{
header('Content-Type: '.get_mime_by_extension($file));
readfile($file);
}
else show_404()
}
else show_404();
}
那么你有一些行添加到您的routes.php文件的文件在您的配置文件夹类似这样
then you have to add some line to your routes.php file in your config folder as like this
$route['my_files/(:any)'] = "my_controller/index/$1";
这将重定向所有请求myfiles的/ bla_bla_bla到my_controller /索引/ bla_bla_bla
this will redirect all request to myfiles/bla_bla_bla to my_controller/index/bla_bla_bla
认为这可能会有帮助:)
think this may help :)
这篇关于如何限制访问文件的文件夹在codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!