Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过头文件下载文件_Php - Fatal编程技术网

Php 通过头文件下载文件

Php 通过头文件下载文件,php,Php,我所需要的帮助是通过文件头下载一个文件,它工作正常,但问题是该文件放在一个文件夹中,我不知道如何给出该特定文件的路径。文件下载总是给我空文件。我的基本sanario是在名为(documents.php)的页面上单击一个链接(图像)下载一个文件,该页面将我发送到另一个名为download.php的页面 这是一个链接(document.php) 下载后,将其发送回documents.php页面,但我要下载的文件位于文件夹uploads/documents中,我不知道如何下载位于uploads/doc

我所需要的帮助是通过文件头下载一个文件,它工作正常,但问题是该文件放在一个文件夹中,我不知道如何给出该特定文件的路径。文件下载总是给我空文件。我的基本sanario是在名为(documents.php)的页面上单击一个链接(图像)下载一个文件,该页面将我发送到另一个名为download.php的页面

这是一个链接(document.php)


下载后,将其发送回documents.php页面,但我要下载的文件位于文件夹uploads/documents中,我不知道如何下载位于uploads/documents中的文件

您无法从下载页面重新定向。php应该就可以了

echo$binary\u文件内容

退出时,它无法发送重定向头


您可以在iframe中打开它,这样您就不会真正离开documents.php页面

您有了一个良好的开端,我将修改您的代码,使其更像以下内容:

$path = $_SERVER['DOCUMENT_ROOT']."/uploads/documents/";
$fullPath = $path.$_GET['title'];

if ($fd = fopen ($fullPath, "r")) {
  $fsize = filesize($fullPath);
  header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
  header("Content-Disposition: attachment; filename='".$_GET['title']."'");
  header("Content-length: $fsize");
  header("Cache-control: private");
  while(!feof($fd)) {
    $buffer = fread($fd, 2048);
    echo $buffer;
  }
}
fclose ($fd);
$path您要为哪个文件夹更新 if语句处理所需的文件调用,并以缓冲区样式回显页面内容

这可以进一步扩展为包含switch语句,具体取决于更改内容类型声明的扩展。由于将内容缓冲到页面中,因此不能调用
标题(“位置”)。或者,由于缓冲,您将得到“由于页面内容已加载,因此无法发送页眉”


我建议在新窗口或页面上的iframe(包装在div中,可以隐藏)中打开它。您也可以将其包含在document.php文件中,而不是调用单独的文件。

我建议您使用此脚本
header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');

header("Content-Disposition: attachment; filename='".$_GET['title']."'");

if(isset($_SESSION)){
  header('Location: documents.php');
}
$path = $_SERVER['DOCUMENT_ROOT']."/uploads/documents/";
$fullPath = $path.$_GET['title'];

if ($fd = fopen ($fullPath, "r")) {
  $fsize = filesize($fullPath);
  header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
  header("Content-Disposition: attachment; filename='".$_GET['title']."'");
  header("Content-length: $fsize");
  header("Cache-control: private");
  while(!feof($fd)) {
    $buffer = fread($fd, 2048);
    echo $buffer;
  }
}
fclose ($fd);