Codeigniter:保护文件夹不被直接访问

时间:2014-09-13 10:15:52

标签: php codeigniter

大家好我有一个与数据库连接的小项目。我拥有一个函数 将文件上载到文件夹中,并保存文件到数据库的路径。 在我的索引页面中,我从数据库中读取文件路径并输出一个表,其中包含指向这些文件的链接以供下 一切正常,文件可以下载,除非, 问题是我忘了保护这个文件夹,昨天意识到我应该以某种方式保护它,因为人们可以直接用链接下载文件 我需要检查用户是否已登录才能下载。

所以我的问题是: 如何使用这些文件保护文件夹不被直接访问,只允许记录用户 能够从该文件夹下载文件

我的上传路径是./uploads/这个文件夹我有htaccess文件

order deny,allow
deny from all

在控制器中我有

public function viewAllPersons() { if($this->ion_auth->logged_in()) { if(!$this->ion_auth->in_group(1)){ show_404(); } else { $data = array(); $data['persons'] = $this->get_persons(); // get all persons from database as array $this->load->view('admin/header_view'); $this->load->view('admin/persons_view',$data); // pass data to the view $this->load->view('admin/footer_view'); } } else { redirect(base_url()); } }

我的视图文件包含此

{

    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
      <h1 class="page-header"><span class="glyphicon glyphicon-th-list"></span> Archive</h1>
      <h2 class="sub-header pull-left">All records db</h2>
     <a href="<?=base_url('dashboard/persons/add');?>" class="add-button pull-right btn btn-success"><span class="glyphicon glyphicon-plus-sign"></span> Add new record</a>

       <form class="navbar-form navbar-right" method="post" action="<?=site_url('dashboard/persons');?>" name="searchform" >
          <div class="form-group">
              <input type="text" id="search" name="search" class="form-control" placeholder="" autocomplete="off"> 
          </div>
          <button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-search"></span></button>
      </form>

      <div class="clearfix"></div>
      <div class="table-responsive">

        <table class="table table-striped">
          <thead>
            <tr>
              <th>#</th>
              <th>Firstname</th>
              <th>Middlename</th>
              <th>Lastname</th>
              <th>ID</th>
              <th>Filename</th>
              <th>Data</th>
              <th>Options</th>
            </tr>
          </thead>
         <tbody>
            <?php 
            $counter = 1;
            foreach($persons as $person) { ?>
            <tr>
              <td><?=$person['person_id'];?></td>
              <td><?=$person['first_name'];?></a></td>
              <td><?=$person['middle_name'];?></td>
              <td><?=$person['last_name'];?></td>
              <td><?=$person['personal_number'];?></td>
              <td><a href="<?php echo base_url('/uploads/'.$person["document_path"]) ; ?>"><?=$person['document_path'];?></a></td> <!-- show links to files for each row !-->
              <td><?=$person['created_on'];?></td>
              <td>
                <a href="<?=base_url('/dashboard/persons/edit/'.$person['person_id'])?>">
                  <span class="glyphicon glyphicon-pencil edit-icon"></span>
                </a>
              </td>
            </tr>
           <?php $counter++; } ?>
          </tbody>
        </table>
        <div class="row">
          <?php if(isset($links)) {
            echo $links;
          }?>
        </div>
      </div>
    </div>
      <?php } ?> }

当我输出文件链接时,我需要检查用户是否已登录,以便能够使用数据库中保存的链接下载文件,这是我的问题

Example of project -- picture

因为人们可以通过直接链接下载文件 实施例

http://website.com/uploads/document.docx

一旦我有htaccess文件我无法下载文件需要一个函数来重写规则或 以某种方式提供对登录用户的访问

我还尝试了一个来自codeigniter的下载帮助程序,但只有在我删除htaccess规则时才下载文件如果htaccess文件中存在规则,则下载帮助程序无法下载该文件。 在此先感谢!

1 个答案:

答案 0 :(得分:1)

您有几种可能性,最简单的方法是控制使用Controller访问这些文件夹。

您需要一个至少包含idpath的文件表。 假设用户A想要文件ABC.jpg(ID为1337):而不是为他http://example.com/uploads/ABC.jpg提供服务,而是给他http://example.com/files?id=1337

此路由调用控制器文件的索引,在索引中可以执行此伪代码:

function index() {
    //Check if logged in
    if (!$user->logged()) {
        redirect('/404');
    }
    //Get file from database
    $file = $this->db->query('SELECT * FROM file WHERE id='.$this->input->get("id"))->result();
    //Then serve the file
    header("Content-type: image/jpeg");
    readfile($file->path);
}

编辑:

我会尝试用其他术语解释它:

  1. 创建一个名为file的上传文件表。像

    这样的东西

    CREATE TABLE file (id INT NOT NULL AUTO_INCREMENT, path VARCHAR(100) NOT NULL, PRIMARY KEY ( id ))

  2. 更新您的person表格,使其没有document_path而是file_id。当您上传文件时,将其路径保存在表file中,然后将文件ID分配给该人(当然是以表格形式)。

  3. 您需要执行<?php echo base_url('/uploads/'.$person["document_path"]);?>

  4. 而不是<?php echo base_url('/file?id='.$pseron['file_id']);?>
  5. 这是因为我们希望用户转到您需要创建File.php(控制器)的特定控制器。

  6. 在此控制器中,函数索引(控制器文件的默认函数)必须执行我在编辑之前显示的内容。这意味着您从数据库中检索基于文件ID的文件路径,然后您提供该文件。这称为PHP服务而不是Apache(默认)文件服务。

相关问题