Rob*_*itt 15
从另一台服务器无需下载到您的服务器:
header('Location: http://thirdparty.com/file.ext');
Run Code Online (Sandbox Code Playgroud)
如果没有在本地下载文件,您在外部服务器上没有授权,因此您必须告诉浏览器要做什么,因此重定向标头,它将告诉服务器直接转到提供的URL,从而加载下载.
从您的服务器,你会做:
if (file_exists($file))
{
if(false !== ($handler = fopen($file, 'r')))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file)); //Remove
//Send the content in chunks
while(false !== ($chunk = fread($handler,4096)))
{
echo $chunk;
}
}
exit;
}
echo "<h1>Content error</h1><p>The file does not exist!</p>";
Run Code Online (Sandbox Code Playgroud)
http://php.net/manual/en/function.header.php#example-3655
如果希望提示用户保存要发送的数据(例如生成的PDF文件),可以使用»Content-Disposition标头提供推荐的文件名并强制浏览器显示保存对话框.
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
Run Code Online (Sandbox Code Playgroud)
Jon*_*nas -1
如果您想提供浏览器定期处理的文件(图像、html...),您需要添加一个标头来更改 MIME 类型,如下所示:
header("Content-Type: application/force-download; name=filename");
如果您无权访问第三方服务器,则别无选择,只能自行下载文件并通过添加标头将其提供给用户
| 归档时间: |
|
| 查看次数: |
19711 次 |
| 最近记录: |