I want to have users be able to upload .pdf and images and restrict the access to these files based on user's privileges. Has anyone done something similar? The basic plan of attach I thought of is a controller checks if the user has privileges to view the document or file. If they have the privileges the document is retrieved and displayed.
My .htacces file would include
#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);
}