2010-06-30 2 views
2

사용자가 .pdf 및 이미지를 업로드하고 사용자의 권한에 따라 이러한 파일에 대한 액세스를 제한 할 수있게하려고합니다. 비슷한 일을 한 사람 있습니까? 기본 부착 계획은 사용자가 문서 나 파일을 볼 권한이 있는지 컨트롤러가 확인하는 것입니다. 권한이 있으면 문서가 검색되어 표시됩니다.Assests에 대한 접근 제한 .pdf .jpg

내 .htacces 파일

#Removes access to the secure_files folder by users. 
RewriteCond %{REQUEST_URI} ^secure_files.* 
RewriteRule ^(.*)$ /index.php?/$1 [L] 


    ------ 
    -SQL 
    ------ 

    ------ 
    - create files table 
    ----- 
    CREATE TABLE `files` (
    id INT NOT NULL AUTO_INCREMENT, 
    file_name VARCHAR(50) NOT NULL, 
    PRIMARY KEY('id') 
    ); 

    ------ 
    - create files table 
    ----- 
    CREATE TABLE `privileges` (
    uesr_id INT NOT NULL, 
    file_id INT NOT NULL, 
    ); 

    ------ 
    - create users table 
    ----- 
    CREATE TABLE `users` (
    id INT NOT NULL AUTO_INCREMENT, 
    name VARCHAR(20) NOT NULL, 
    email VARCHAR(50) NOT NULL, 
    password CHAR(40) NOT NULL, 
    PRIMARY KEY('id') 
    ); 

    /* 
    * pseudo-Codeigniter code. I can edit this to make 
    * it pure PHP if that would be more helpful 
    * 
    */ 
    public function get_user_files($filename) 
    { 
     //this is set during login 
     $user_id = $this->session->userdata('user_id'); 

     //check to see if the user has privileges to access the file and gets the file name 
     $query = $this->db->join('privileges','privileges.id = files.id') 
        ->select('files.file_name') 
        ->where('privileges.user_id',$user_id) 
        ->where('files.file_name',$file_name) 
        ->limit(1) 
        ->get('files'); 

    $file = $query->row()->files.file_name; 

    if($file) 
    { 
    //user has privileges to access the file so include it 

    //WHAT DO I DO HERE!?! 
    //start Would this work? 
    $handle = fopen($file, "rb"); 
    $data['file'] = fread($handle, filesize($file)); 
    fclose($handle); 
    //end would this work? 
    } 
    $this->load->view('files',$data); 
} 

답변

1

가 쉽고 보안 솔루션은 다음과 같은 스키마를 사용하는 것이 포함됩니다. 이렇게하면 업로드 된 파일에 대한 액세스를 제한하고 .php 파일을 업로드하는 문제를 피할 수 있습니다. 파일 크기는 16MB 크기의 longblob 크기로 제한됩니다.

$user_file=file_get_contents($_FILES['user_file']['tmp_name']); 
//Make the file binary safe 
$user_file=mysql_real_escape_string($user_file); 
$file_name=mysql_real_escape_string($_FILES['user_file']['name']); 
$user_file=mysql_real_escape_string($user_file); 

mysql_query("insert into files (user_id,file_name,file)values ("$_SESSION[user_id]","$file_name","$user_file"); 

액세스 제어는 파일의 USER_ID를 선택하여 적용 할 수 있습니다 : 여기

CREATE TABLE `files` (
id INT NOT NULL AUTO_INCREMENT, 
user_id INT NOT NULL, 
file_name text NOT NULL, 
file LONGBLOB NOT NULL, 
PRIMARY KEY('id') 
); 

은 파일을 업로드 PHP 코드입니다. 사용자 파일의 content-type은 스푸핑하기 쉽기 때문에 $_FILE[name][type]을 신뢰하지 마십시오. 그러나 사용자가 파일을 다운로드하게 할 때이 파일이 올바른 유형이어야합니다. 예를 들어 pdf에는이 content-type이 있어야합니다.

+0

아, 간단한 해결책은 항상 최고입니다! 나는 이미지와 문서를 DB에 저장하지 말라는 사람들과 항상 동의했지만,이 경우 액세스 제어의 주요 관심사이므로 최상의 솔루션입니다. 이 문제를 해결해 주셔서 감사합니다. – John

0

header('Content-type: application/pdf'); 왜 그냥 검사 권한을 다음 readfile 기능으로 파일 내용을 뱉어, 모든 액세스를 차단 .htaccess있는 디렉토리에 넣어 적절한 헤더를 보낼 PHP 스크립트를 사용할 수 있습니까?

관련 문제