如何在PHP中下载现有文件 [英] How to download existing file in PHP

查看:104
本文介绍了如何在PHP中下载现有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器上有一个pdf文件。我想创建这样的链接,用户可以点击它并下载该pdf文件。我正在使用Zend框架工作与Php。

解决方案

将此代码放在php文件中,并将其称为f.e。 download.php:

 <?php 

$ fullPath =路径/到/你/ file.ext;

if($ fd = fopen($ fullPath,r)){

$ fsize = filesize($ fullPath);
$ path_parts = pathinfo($ fullPath);
$ ext = strtolower($ path_parts [extension]);

header(Content-type:application / pdf);
header(Content-Disposition:attachment; filename = \$ path_parts [basename]。\);
header(Content-length:$ fsize);
header(Cache-control:private);

while(!feof($ fd)){
$ buffer = fread($ fd,2048);
echo $ buffer;
}
}

fclose($ fd);
退出;

?>

示例:将此类链接放入提供文件下载的文档中: / strong>

 < a href =download.php?download_file = some_file.pdf>在这里下载< / a> ; 

详细信息:



http://www.finalwebsites.com/forums/topic/ php-file-download


I have a pdf file on my server. I want to create such link that, user can click on it and download that pdf file. I am using Zend frame work with Php.

解决方案

place this code inside a php file and call it f.e. "download.php":

<?php

$fullPath = "path/to/your/file.ext";

if ($fd = fopen ($fullPath, "r")) {

    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);

    header("Content-type: application/pdf");
    header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");            
    header("Content-length: $fsize");
    header("Cache-control: private");

    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>

More Detail:

http://www.finalwebsites.com/forums/topic/php-file-download

这篇关于如何在PHP中下载现有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆