[英]PHP create file on server to download
我尝试在Web服务器上创建文件,然后下载。 使用此代码
<?php
$text = "test";
$filename = 'Testfile.txt';
file_put_contents($filename, $text, FILE_APPEND | LOCK_EX) or die ("Unable to open file!");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
readfile($filename);
?>
问题是,我总是得到“无法打开文件!” ..如果我尝试不带或死了.....,它将下载文件,但为空。 我试图在服务器上创建一个具有所有权限的文件夹(chmod 777 ...),但是比在该位置加载/创建文件,但这也行不通。 :(
$text = "test";
$filename = 'Testfile.txt';
if (is_writable($filename)) {
file_put_contents($filename, $text, FILE_APPEND | LOCK_EX) or die ("Unable to open file!");
} else {
echo "You Do not have write permission to this FILE.";
}
使用filewrite
并检查文件夹是否具有写权限
$text = '123';
$filename = "Testfile.txt";
$fh = fopen($filename, 'w') or die("can't open file");
fwrite($fh, $text);
fclose($fh);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
readfile($filename);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.