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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分