首页 > 后端开发 > php教程 > php下载文件的代码示例

php下载文件的代码示例

高洛峰
发布: 2023-03-04 07:30:01
原创
1326 人浏览过

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?php

$file = &#39;monkey.gif&#39;;

 

if (file_exists($file)) {

header(&#39;Content-Description: File Transfer&#39;);

header(&#39;Content-Type: application/octet-stream&#39;);

header(&#39;Content-Disposition: attachment; filename=&#39;.basename($file));

header(&#39;Content-Transfer-Encoding: binary&#39;);

header(&#39;Expires: 0&#39;);

header(&#39;Cache-Control: must-revalidate, post-check=0, pre-check=0&#39;);

header(&#39;Pragma: public&#39;);

header(&#39;Content-Length: &#39; . filesize($file));

ob_clean();

flush();

readfile($file);

exit;

}

?>

登录后复制

以上代码是下载代码
接下来贴一段在线预览pdf文件的代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<?php

public function fddAction()

{

// get attachment location

$attachment_location = $_SERVER["DOCUMENT_ROOT"] . "/pdf/fdd/sample.pdf";

 

if (file_exists($attachment_location)) {

// attachment exists

 

// send open pdf dialog to user

header(&#39;Cache-Control: public&#39;); // needed for i.e.

header(&#39;Content-Type: application/pdf&#39;);

header(&#39;Content-Disposition: inline; filename="sample.pdf"&#39;);

readfile($attachment_location);

die(); // stop execution of further script because we are only outputting the pdf

 

} else {

die(&#39;Error: File not found.&#39;);

}

}

?>

登录后复制

更多php下载文件的代码示例相关文章请关注PHP中文网!

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板