开发者

set file permissions in header before forced download in php?

开发者 https://www.devze.com 2022-12-10 10:56 出处:网络
Im generating a file to present to the user for download, but the server isnt letting me open it, because it needs to have permissions of 777 before it can do so.

Im generating a file to present to the user for download, but the server isnt letting me open it, because it needs to have permissions of 777 before it can do so.

Here is my code

$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);

any way to set the permissions of this before before i open it, or how would i go about doing this? The file has not been created yet, so开发者_开发问答 it might be kind of a endless loop. Should i make the file, then open it, or what?


If you don't need the temporary file you're writing, $name, then just stream it right out without creating a file at all.

You can instead just output all your headers and echo the output.


If name of the file is fixed then

  • create a empty file on server
  • set its permission.
  • open it from php
0

精彩评论

暂无评论...
验证码 换一张
取 消