Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.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_File_Download - Fatal编程技术网

Php 如何开始下载文件?

Php 如何开始下载文件?,php,file,download,Php,File,Download,可能重复: 所以我在卖东西,我想在用户进入感谢页面时开始下载文件 文件的位置存储在$install中,现在当用户访问谢谢.php时,如何自动启动文件下载 谢谢。您想做的大部分事情实际上都是用Javascript完成的 PHP代码将提供感谢页面,加载后,您将希望使用以下标题将页面中隐藏的iFrame定向到提供文件作为下载的页面: header("Content-Type: application/octet-stream"); header("Content-Disposition: attac

可能重复:

所以我在卖东西,我想在用户进入感谢页面时开始下载文件

文件的位置存储在
$install
中,现在当用户访问
谢谢.php
时,如何自动启动文件下载


谢谢。

您想做的大部分事情实际上都是用Javascript完成的

PHP代码将提供感谢页面,加载后,您将希望使用以下标题将页面中隐藏的iFrame定向到提供文件作为下载的页面:

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=yourfile.txt");
readfile($pathToFile);


javascript重定向的替代方法是在HTML中使用标记。它甚至在禁用javascript时也能工作。

可能打开位置为=$install@experimentX不,那不会是一次看不见的经历。。嗯,你打算控制下载吗?已经问过很多次了,比如这里的Javascript?为什么?
位置
重定向
标题有什么问题?为什么他需要使用“隐藏的iframe”?@binaryLV,因为我想他希望人们看到并阅读Thanke.php。如果你使用重定向,他们将永远看不到页面。为什么不呢?打开“Thank.php”页面并发送“refresh”标题(而不是我之前写的“redirect”)。有什么问题?“刷新”也可以用作HTTP头。
<?php
// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_file'];
if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {
        case "pdf":
        header("Content-type: application/pdf"); // add here more headers for diff. extensions
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
        break;
        default;
            header("Content-type: application/octet-stream");
            header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    }
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
exit;
// example: place this kind of link into the document where the file download is offered:
// <a href="download.php?download_file=some_file.pdf">Download here</a>
?>