我必须强制下载一个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);