hcwzwx 发表于 2017-4-9 09:30:07

php点击链接下载图片或其他类型文件的代码

php点击链接下载图片或其他类型文件的代码
<?php
$filename = $_GET['filename'];
header("Content-type: application/octet-stream");
header("Content-Length: ".filesize($filename));
header("Content-Disposition: attachment; filename=$filename");
$fp = fopen($filename, 'rb');
fpassthru($fp);
fclose($fp);

某一种类型文件下载举例
<?php
// 这样将会直接输出一个 PDF 文件
header('Content-type: application/pdf');
// 这样做就会提示下载 PDF 文件 downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// 这是 original.pdf 的源文件
readfile('original.pdf');
?>

如果只是要将文件输出到标准输出,可以使用 readfile() 会比用 fopen() 更好。
页: [1]
查看完整版本: php点击链接下载图片或其他类型文件的代码