在 php 中,可以使用 urlencode() 函数来对文件名进行编码。但是,在 laravel 中我们可以使用内置的 str::slug() 方法来完成这个任务。
str::slug() 方法可以将一个字符串转化为 url 友好的 slug 格式。但是,我们可以选择不传入第二个参数,以保留原有字符,只进行 url 编码。
下面是一个示例代码:
public function downloadfile($filename) { $fullpath = storage_path('app/download/' . $filename); $headers = [ 'content-type' => 'application/octet-stream', ]; $escapedfilename = str::slug($filename, ''); return response()->download($fullpath, $escapedfilename, $headers);}
在上面的代码中,我们使用了 str::slug() 方法将文件名进行了编码,并将编码后的字符串作为第二个参数传递给 download() 方法。
通过这种方式,我们可以确保 laravel 在处理下载文件时能够正确处理包含中文字符的文件名。
以上就是laravel 下载功能不能用中文的详细内容。
