header() 下载后没有任何内容的文件

发布于 2024-11-29 05:56:22 字数 466 浏览 0 评论 0原文

我正在尝试创建一个下载,以便用户单击“向下”,它将某个文件从他们的帐户下载到他们的计算机,我目前正在使用这个:

header('Content-Disposition: attachment; filename=' . basename("users/$username/$file_folder/$file_name"));
header("Content-Type:" .$file_type);
header("Content-Description: File Transfer");
header("Cache-control: private");
header("Connection: close");
header("Content-Length: ".$file_size);

问题是,文件正在下载,但它只是空的,文件中没有内容

之前的代码只是一个 if() 和一个带有数据库记录的 while 循环。 提前致谢。

I'm trying to create a download so that a user clicks on "down" it downloads a certain file from their account to their computer, I'm currently using this:

header('Content-Disposition: attachment; filename=' . basename("users/$username/$file_folder/$file_name"));
header("Content-Type:" .$file_type);
header("Content-Description: File Transfer");
header("Cache-control: private");
header("Connection: close");
header("Content-Length: ".$file_size);

The problem is, the file is downloading, but it's just empty, there is no content in the file

The code before this is just an if() and a while loop with database records.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美胚控场 2024-12-06 05:56:22

:(除非文件非常大,在这种情况下您会将其分块)

$filename = 'path/to/file/file_name.txt';

echo file_get_contents($filename);

或者您可以使用要放入文件中的数据填充变量并简单地回显它,如下所示:

$data = "begin\n";
$data .= "first line\n";
$data .= "another line\n";
$data .= "last line";

echo $data;

您缺少如下所示的内容 会在你的标题之后放在那里。希望这有帮助。

You are missing something like below: (unless the file is very large, in which case you would chunk it out)

$filename = 'path/to/file/file_name.txt';

echo file_get_contents($filename);

Alternatively you could populate a variable with the data you want put out into the file and simple echo it out like so:

$data = "begin\n";
$data .= "first line\n";
$data .= "another line\n";
$data .= "last line";

echo $data;

The content would be put out there AFTER your headers. Hope this helps.

夜夜流光相皎洁 2024-12-06 05:56:22

该文件是空的,因为您从未输出该文件。这些 header 调用只是标头,您仍然需要一个正文才能正确下载文件。您可以使用 file_get_contents回显文件内容。

header('Content-Disposition: attachment; filename=' . basename("users/$username/$file_folder/$file_name"));
header("Content-Type:" .$file_type);
header("Content-Description: File Transfer");
header("Cache-control: private");
header("Connection: close");
header("Content-Length: ".$file_size);

// echo the file, this will make the download work
echo file_get_contents("users/$username/$file_folder/$file_name"); 

The file is empty, because you never output the file. These header calls are just the header, you still need a body for a file to be correctly downloaded. You can use file_get_contents to echo the file contents.

header('Content-Disposition: attachment; filename=' . basename("users/$username/$file_folder/$file_name"));
header("Content-Type:" .$file_type);
header("Content-Description: File Transfer");
header("Cache-control: private");
header("Connection: close");
header("Content-Length: ".$file_size);

// echo the file, this will make the download work
echo file_get_contents("users/$username/$file_folder/$file_name"); 
音栖息无 2024-12-06 05:56:22

发送标头后,您需要实际推出文件内容...

请参阅此
http://php.net/manual/en/function.file-put -contents.php

after you send the headers you need to actually push out the file content...

see this
http://php.net/manual/en/function.file-put-contents.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文