dongyinglan8707 2018-01-16 15:49
浏览 80
已采纳

CodeIgniter3 - 存储PDF文件的位置?

I ma using CodeIgniter3 to build projects. However I am not sure where is the best place to store generated PDF files. I do not want to keep them in MySQL due to its size.

I do not want people to access direct links like: http://mydomain/pdf/file.pdf but I would like to give this kind of access for codeigniter logged users.

What is the best way to do that?

  • 写回答

2条回答 默认 最新

  • dongxian6653 2018-01-16 20:01
    关注
    1. Create site/pdf_files/.htaccess file and add this line:

      Deny from all
      
    2. Make router config as follows

      /*
       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. and then while serving pdf to user prefer codeigniter file helper like below

      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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀