CodeIgniter3 - 存储PDF文件的位置?

时间:2018-01-16 15:49:30

标签: php codeigniter file pdf codeigniter-3

我使用CodeIgniter3来构建项目。 但是我不确定存储生成的PDF文件的最佳位置在哪里。 由于它的大小,我不想让它们保留在MySQL中。

我不希望别人访问直接链接,例如:http://mydomain/pdf/file.pdf 但我想为codeigniter登录用户提供这种访问权限。

最好的方法是什么?

2 个答案:

答案 0 :(得分:1)

  1. 创建site/pdf_files/.htaccess文件并添加以下行:

    Deny from all
    
  2. 按如下方式进行路由器配置

    /*
     anything in second segment of pdf controller will be routed to
     index method 
    
     serve file like : 
     http://example.com/pdf/myfile.pdf
    */
    $route['pdf/(:any)'] = "pdf/index/$1";
    
    /*
     Note: if you got some more methods in same controller prefer below one
     because, with above one, anything in second segment will be routed to 
     index method  
    
     $route['pdf/display/(:any)'] = "pdf/index/$1";
    
     and serve files like
     http://example.com/pdf/display/myfile.pdf
    */
    
  3. 然后在向用户提供pdf时更喜欢codeigniter文件助手,如下所示

    class Pdf extends CI_Controller
    {
    
    function download_file($path, $name)
    {
      // make sure it's a file before doing anything!
      if(is_file($path))
      {
    
        // get the file mime type using the file extension
        $this->load->helper('file');
    
        header('Content-Type: '.get_mime_by_extension($path));  
        header('Content-Disposition: attachment; filename="'.basename($name).'"');  // Add the file name
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: '.filesize($path)); // provide file size
        header('Connection: close');
        readfile($path); 
        die();
      }
    }
    
     public function index($file)
     {
        // here you check whether user logged in or not
    
         $this->download_file('pdf_files/'.$file, $file);
     }
    }
    

答案 1 :(得分:0)

将PDF文档根目录外的PDF存储在非公开的位置。

您可以使用应用程序逻辑来允许登录用户查看/访问:

class pdf extends ci_controller{

     public function view()
     {
        //check if user is logged in and has permission to view
        //set proper header information
        //readfile('non_public_path\to\pdf);
     }

}

只要你的pdf在你的公共目录之外 - 发送请求 domain.com/pdf/view只会在用户登录并有权查看时呈现pdf。