[英]Codeigniter : Protect folder from direct access
大家好,我有一個與數據庫連接的小項目。我擁有一個 function 用於將文件上傳到文件夾並將文件路徑保存到數據庫。 在我的索引頁面中,我從數據庫和 output 讀取文件路徑,其中包含指向這些文件的鏈接以供下載。 一切正常並且可以下載文件,除非問題是我忘記保護這個文件夾並且昨天意識到我應該以某種方式保護它,因為人們可以直接使用鏈接下載文件,我需要檢查用戶是否已登錄能夠下載它。
所以我的問題是:如何保護包含這些文件的文件夾不被直接訪問,並使只有登錄的用戶能夠從該文件夾下載文件
我的上傳路徑是 ./uploads/ 在這個文件夾里面我有 htaccess 文件
order deny,allow
deny from all
在 controller 我有
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 } ?> }
這是我的問題,當我 output 鏈接到文件時,我需要檢查用戶是否已登錄才能使用保存在數據庫中的鏈接下載文件
因為人們可以通過直接鏈接下載文件示例
http://website.com/uploads/document.docx
一旦我有了 htaccess 文件,我就無法下載文件,可能需要 function 來重寫規則或以某種方式授予登錄用戶的訪問權限
我還嘗試了 codeigniter 的下載幫助程序,但僅當我刪除 htaccess 規則時才下載文件如果 htaccess 文件中存在規則,則下載幫助程序無法下載文件。 提前致謝!
您有幾種可能性,最簡單的一種是使用 Controller 控制對這些文件夾的訪問。
你需要一個最少的文件表: id
, path
。 假設用戶 A 想要文件 ABC.jpg(id 為 1337):而不是為他服務http://example.com/uploads/ABC.jpg ,你給他http://example.com/files?id= 1337 .
此路由調用 controller 文件的索引,在索引中您可以執行以下偽代碼:
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);
}
編輯:
我將嘗試用其他術語來解釋它:
創建一個名為file
的上傳文件表。 就像是
CREATE TABLE file (id INT NOT NULL AUTO_INCREMENT, path VARCHAR(100) NOT NULL, PRIMARY KEY ( id ))
更新您的person
表,使其沒有document_path
而是file_id
。 當您上傳文件時,您將其路徑保存在表格file
中,然后將文件 id 分配給人員(當然在表格人員中)。
您需要執行<?php echo base_url('/file?id='.$pseron['file_id']);?>
<?php echo base_url('/uploads/'.$person["document_path"]);?>
<?php echo base_url('/file?id='.$pseron['file_id']);?>
這是因為我們希望用戶將 go 到特定的 controller,您需要創建File.php
(控制器)。
In this controller, the function index (the default function of the controller File) must do something like I showed you before the edit. 這意味着您從數據庫中檢索基於文件 ID 的文件路徑,然后提供該文件。 這稱為 PHP 服務而不是 Apache(默認)文件服務。
我遇到了同樣的問題,因為我的文件是由用戶直接訪問的。 特別是上傳文件和圖像。 我正在研究 PHP 框架。
我只是在 .htaccess 文件中添加了一行代碼。 它完成了。 如果您的網站文件夾中沒有,則創建一個 .txt 文件並將其命名為 .htaccess(如果您沒有使用任何框架)。 然后打開.htccess文件並簡單地寫:選項-索引注意:不要將.htaccess文件放在任何文件夾中,只需在htdocs中打開您的網站文件夾並創建.htaccess
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.