2012-09-03 109 views
2

您好我已经使用codeigniter来开发我的网站,但是当我的网站在google中搜索时,google会显示特定文件夹中文件的链接(pdf),用户可以查看这些文件(pdf)直接没有登录。我想限制谷歌直接显示这些文件的链接。 例如:www.mywebsite/myfolder/myfile如何限制对codeigniter文件夹中的文件的访问

请指导我如何完成此操作。谢谢。

回答

0

基本上你需要在public_html文件夹之外托管文件 - 而是通过php readfile()将它们提供给用户。

这样的事会工作

function user_files($file_name = "") 
{ 
    // Check if user is logged in 
    if($this->user->logged_in()) 
     { 
      // Now check the filename is only made up of letters and numbers 
      // this helps prevent against file transversal attacks 
      if (preg_match('^[A-Za-z0-9]{2,32}+[.]{1}[A-Za-z]{3,4}$^', $file_name))) 
      { 
       // Rewrite the request to the path on my server 
       $file = '../my_folder/'.$file_name; 

       // Now check if file exists 
       if (file_exists($file)) 
       { 
        // Serve the file 
        header('Content-Type: '.get_mime_by_extension($file)); 
        readfile($file); 
       } 
      } 
     } 
    } 
} 

注意:您需要小心目录横移的 - 所以你应该把一些检查该文件名只由字母

+0

我已经使用了上述方法,但是当我们从网上下载pdf并尝试打开时,它说打开这个文件时出现错误,但是这段代码在localhost中正常工作。 –

3

稍加修改从以前answer

放置的.htaccess在您的文件夹与内容

order deny, allow 
deny from all 

这将拒绝对该特定文件夹的所有访问。

对于访问文件的编写一个函数与车身控制器 -

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文件文件这样

$route['my_files/(:any)'] = "my_controller/index/$1"; 

这会将所有请求重定向到myfiles/bla_bla_bla到my_controller/index/bla_bla_bla

认为这可能有所帮助:)