【发布时间】:2009-10-28 17:31:51
【问题描述】:
我正在生成一个文件以供用户下载,但服务器不允许我打开它,因为它需要拥有 777 的权限才能这样做。
这是我的代码
$fh = fopen("$name", 'w') or die("can't open file");
fwrite($fh, $data);
fclose($fh);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$name").";");
header("Content-Disposition: attachment; filename=$name");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
readfile($name);
在我打开它之前有什么方法可以设置它的权限,或者我将如何去做?该文件尚未创建,因此可能是一种无限循环。我应该制作文件,然后打开它,还是什么?
【问题讨论】:
-
为什么不直接使用
echo $data?还是您还需要该文件用于其他用途? -
卢卡斯·拉林斯基是对的。您应该在将文件发送给用户的标头之后回显数据。而且它不会将它写入您的服务器,从而节省您的硬盘。