【问题标题】:What is the equivalent of force_download methode in codeIgniter 4codeIgniter 4中的force_download方法的等价物是什么
【发布时间】:2020-12-02 00:41:52
【问题描述】:

我正在尝试允许从 codeIgniter 4 中的可写路径下载文件 此代码不匹配

helper('download');
            // read file contents
            //$data = file_get_contents(WRITEPATH.'uploads/'.$filename);
    
             $data = file_get_contents(base_url().'/uploads/'.$filename, TRUE);
            force_download($filename, $data);

我收到此错误: 调用未定义的函数

App\Controllers\force_download()

谢谢你的帮助

【问题讨论】:

    标签: php file codeigniter file-get-contents force-download


    【解决方案1】:

    如果您有文件的直接路径,您可以使用来自$responsedownload() 方法

    return $response->download('/path/to/photo.jpg', null);
    

    您可以选择使用setFileName() 设置文件发送到浏览器时的名称

    return $response->download('/path/to/photo.jpg', null)->setFileName('some.jpg');
    

    Reference: Force File Download

    【讨论】:

      【解决方案2】:

      在 CI 3 中,我们使用 helper 来加载我们的下载功能,方法是 force_download。 在 CI 4 中,我们使用 $response 来下载我们的文件,下面是下载文件的代码:

      public function download($id){
              $superadminModel = model('App\Models\SuperAdminModel');
              $fileinfo = $superadminModel->download($id);// get name of file using ID
              $file = ROOTPATH.'/uploads/documents/'.$File['doc'];//file location+filename
              return $this->response->download($file, null);//download file
          }
      

      ROOTPATH.'/uploads/documents/' 您的文件的此位置可能不同,因此请添加文件的位置而不是此位置

      【讨论】:

      • 您能否建议如何通过 REST API 下载?
      • 对不起,我没有学过RUST,当时我在学CI4和3
      猜你喜欢
      • 2020-05-16
      • 1970-01-01
      • 2020-07-24
      • 2011-05-26
      • 2014-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多