0

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

2 回答 2

0

PHP 有一个名为chmod()的函数用于更改文件权限。

用法: chmod($path,0777);

以下链接 1链接 2中的更多信息。

于 2016-01-11T07:11:03.597 回答
0

解决了。由于问题出在客户端,文件被保存在某个文件夹中,默认情况下所有文件都是只读的。因此,当我更改下载文件夹位置时,它现在可以完美运行。所以代码已经是绝对正确的。

于 2016-01-11T07:21:59.257 回答