2014-09-13 118 views
2

好傢伙我有一個小的項目有database.I連自己的 文件上傳到一個文件夾的功能,也將文件保存到數據庫路徑。 在我的索引頁面中,我從數據庫中讀取文件路徑,並輸出一個包含這些文件鏈接的表格供下載。 一切工作正常和文件能夠被下載的,除非 的問題是,我忘了,以確保該文件夾和昨天才意識到,我應該以某種方式保護它,因爲人們可以通過鏈接 直接下載文件,我需要檢查,如果用戶登錄後即可下載。笨:直接訪問保護文件夾

所以我的問題是: 如何保護文件夾直接訪問這些文件,並只記錄用戶 能夠從該文件夾

我上傳的路徑這裏面./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文件我真的無法下載文件時,也許需要一個功能重寫規則或 授予訪問權限的用戶登錄莫名其妙

我還試圖從笨,但下載下載幫手文件只有當我刪除htaccess規則如果htaccess文件內存在規則下載助手無法下載文件。 在此先感謝!

回答

1

你有幾個可能性,最簡單的就是讓這些文件夾與控制器訪問控制。

你需要用最小的文件表: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_pathfile_id代替。當你上傳一個文件時,你把它保存在表file的路徑中,然後將文件ID分配給該人(當然是表中的人)。

  3. 相反的<?php echo base_url('/uploads/'.$person["document_path"]);?>,你需要做的<?php echo base_url('/file?id='.$pseron['file_id']);?>

  4. 這是因爲我們希望用戶去,你需要創建File.php(控制器)特定的控制器。

  5. 在此控制器中,函數index(控制器File的默認函數)必須執行類似於編輯前顯示的內容。這意味着你從數據庫中檢索基於文件ID的文件路徑,然後服務該文件。這被稱爲PHP服務而不是Apache(默認)服務文件。

+0

我真的不'噸知道如何去做它了棘手的問題,我.. – DarkKnight 2014-09-16 09:39:23

+0

哪一部分你不知道該怎麼辦? – Kalzem 2014-09-16 12:46:51

+0

以上我添加了附加信息 – DarkKnight 2014-09-16 14:56:26