【发布时间】:2016-01-11 06:33:21
【问题描述】:
我必须强制下载一个 excel 文件。文件正在按照我的意愿完美下载。但问题是,当我要更改并保存文件的内容时,出现错误,就像我无权访问更改文件一样。因此,我必须在下载文件时设置文件权限。但我不知道怎么做。如果有人可以回答,那么将不胜感激。这是我的代码,可以完美地下载文件。
$filename = 'myfile.xlsx';
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data"; //$header and $data are the array contains data with '\t' (tabular form data).
当我更改我的代码时。但是还是有问题。问题是此代码将权限设置为仅在服务器端创建的文件,而不是在客户端下载的文件。这是更新的代码。
$filename = 'myfile.xlsx';
$fp = fopen('/var/www/html/cakephp-3.0/webroot/downloads/' . $filename, 'w');
fwrite($fp, "$header\n$data");
fclose($fp);
chmod('/var/www/html/cakephp-3.0/webroot/downloads/' . $filename, 0777);
// Generate Excel File
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");
readfile('/var/www/html/cakephp-3.0/webroot/downloads/' . $filename);
【问题讨论】:
-
在
<a>标记中设置download属性,例如<a href="url" download>
标签: php file-permissions force-download