【问题标题】:Codeigniter 4 - How to custom the file name for the downloaded file (with different file extension like jpg, jpeg / pdf)?Codeigniter 4 - 如何自定义下载文件的文件名(具有不同的文件扩展名,如 jpg、jpeg / pdf)?
【发布时间】:2021-10-26 06:34:33
【问题描述】:

我刚开始学习 codeigniter 4。如何修改下面的代码以使用自定义文件名和正确的扩展名从数据库下载文件。谢谢。

public function download($payment_id){


    $invoicePaymentModel = new InvoicePaymentModel();
    $file = $invoicePaymentModel ->find($payment_id);

    return $this->response->download('uploads/'.$file['payment_proof'], NULL)->setFileName("testing");


    
}

【问题讨论】:

  • 这应该是工作方法,你得到什么错误?你能发布错误或描述你得到的行为吗?

标签: codeigniter codeigniter-4


【解决方案1】:

Force File Download

如果将第三个参数设置为布尔值true,那么实际文件 MIME 类型(基于文件扩展名)将被发送,这样如果 您的浏览器具有该类型的处理程序 - 它可以使用它。

// ...
return $this->response->download('uploads/'.$file['payment_proof'], NULL, true);
// ...

使用可选的setFileName() 方法更改文件名 被发送到客户端的浏览器:

// ...
$path = 'uploads/' . $file['payment_proof'];

return $this->response->download($path, NULL)
    ->setFileName("testing" . "." . pathinfo($path, PATHINFO_EXTENSION));
// ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多