php 返回一个文件 How to return a file in PHP

PHP · Fecmall · 于 1年前 发布 · 779 次阅读

1.返回一个zip文件

https://stackoverflow.com/questions/6175533/how-to-return-a-file-in-php

 $attachment_location = $_SERVER["DOCUMENT_ROOT"] . "/file.zip";
        if (file_exists($attachment_location)) {

            header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
            header("Cache-Control: public"); // needed for internet explorer
            header("Content-Type: application/zip");
            header("Content-Transfer-Encoding: Binary");
            header("Content-Length:".filesize($attachment_location));
            header("Content-Disposition: attachment; filename=file.zip");
            readfile($attachment_location);
            die();        
        } else {
            die("Error: File not found.");
        } 

2.下载文件

https://www.phptutorial.net/php-tutorial/php-download-file/

<?php

$filename = 'readme.pdf';

if (file_exists($filename)) {
	header('Content-Description: File Transfer');
	header('Content-Type: application/octet-stream');
	header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
	header('Expires: 0');
	header('Cache-Control: must-revalidate');
	header('Pragma: public');
	header('Content-Length: ' . filesize($filename));
	readfile($filename);
	exit;
}

3.

共收到 1 条回复
Fecmall#11年前 0 个赞

1.category.xml.gz 类型:application/x-gzip

2.sitemap.xml 类型:application/xml

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics