【发布时间】:2022-11-19 13:34:44
【问题描述】:
我在我的 vps 服务器中创建了一个包含一些图像的 download.php 文件,如果用户使用此文件请求该文件,该文件将保存到设备中。但这会创建一个空文件。这是代码。
<?php
if(isset($_GET['file']))
{
$filename = $_GET["file"];
if(preg_match('/^[^.][-a-z0-9_.]+[a-z]$/i', $file)){
$filepath = "images/" . $file;
if(file_exists($filepath)) {
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$filename."\"");
readfile($filepath);
}
}
?>
我已经更正了拼写错误并删除了 echo still ,它是一样的,当在服务器上调用 download.php?file=abstract.jpg 时下载文件。
【问题讨论】:
-
打字错误,额外的下划线:
readfile($filepath); -
你怎么服务这个?你如何调用它?这个问题实际上缺少minimal reproducible example,因此无法回答。我也看不到任何保存文件的地方。请作为这里的新用户,也带上tour并阅读How to Ask。
-
你不想
echo readfile。它自己回声。 -
另请注意,您不应该
echo readfile(),只需readfile()本身,因为它已经将文件转储到输出缓冲区。通过同时调用 echo,您将输出额外的信息,在本例中,是 readfile() 的返回值,即输出的字节数。
标签: php