【问题标题】:Set Permission on forced download file设置强制下载文件的权限
【发布时间】: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


【解决方案1】:

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

用法:chmod($path,0777);

更多信息请关注link 1link 2

【讨论】:

  • @13ey,因为我也使用了 chmod()。但我只允许访问在服务器端创建的文件,而不是在客户端下载的文件。
  • 查看我添加的其他链接。您也可以在 $path 变量中提供本地路径以更改其权限。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 2015-10-18
    • 1970-01-01
    相关资源
    最近更新 更多