2012-09-03 160 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

認爲這可能有所幫助:)