【问题标题】:How to restrict access to files in a folder in codeigniter如何限制对codeigniter文件夹中文件的访问
【发布时间】:2012-08-28 03:25:12
【问题描述】:

您好,我使用 codeigniter 开发我的网站,但是当我在 google 中搜索我的网站时,google 会显示指向特定文件夹中文件 (pdf) 的链接,用户无需登录即可直接查看这些文件 (pdf)。我想限制谷歌直接显示这些文件的链接。 例如:www.mywebsite/myfolder/myfile

请指导我如何做到这一点。谢谢。

【问题讨论】:

    标签: security .htaccess codeigniter robot


    【解决方案1】:

    对之前的answer 稍作修改:

    将 .htaccess 放入包含内容的文件夹中

    order deny, allow
    deny from all
    

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

    为了访问文件,在控制器中编写一个带有 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 文件中添加一些行

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

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

    认为这可能会有所帮助:)

    【讨论】:

      【解决方案2】:

      基本上,您需要将文件托管在 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);
                     }
                  }
              }
          }
      }
      

      注意:您需要小心目录转换 - 因此您应该检查文件名是否仅由字母组成

      【讨论】:

      • 我已经使用了上述方法,但是当我们从网上下载pdf并尝试打开时,它说打开此文件时出错,但此代码在localhost中可以正常工作。
      【解决方案3】:

      只需在readfile($file) 之前添加以下代码。这将有助于浏览器读取图像和视频文件。

      ob_clean();
      flush();
      

      完整示例

      if($this->user->logged_in())
      {
          if (file_exists($file))
          {
              header('Content-Description: File Transfer');
              header('Content-Type: application/octet-stream');
              header('Content-Disposition: attachment; filename='.basename($file));
              header('Content-Transfer-Encoding: binary');
              header('Expires: 0');
              header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
              header('Pragma: public');
              header('Content-Length: '.filesize($file));
              ob_clean();
              flush();
              readfile($file);
              exit;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-11
        • 1970-01-01
        • 1970-01-01
        • 2017-09-17
        • 2016-05-05
        • 2010-10-26
        相关资源
        最近更新 更多