【问题标题】:How to force-downloads in PHP?如何在 PHP 中强制下载?
【发布时间】:2015-01-26 06:51:37
【问题描述】:

如何强制下载多个链接?如果用户单击链接 1,他们会下载 pdf1.pdf。如果用户单击链接 2,他们会下载 pdf2.pdf。有没有办法检查他们点击了哪个链接?

【问题讨论】:

标签: php force-download


【解决方案1】:

试试

<?php
$FileName = 'file.pdf';
header('Content-disposition: attachment; filename="'.$FileName.'"');
readfile($FileName);

【讨论】:

  • 如果我尝试这样做并且我有 $FileName = 'file1.pdf'; $FileName = 'file2.pdf';它不断带回 file1.pdf 而不是 file2.pdf
【解决方案2】:
$file_url = 'your_file.someting';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
readfile($file_url);

附:为您的文件使用正确的内容类型,例如 application/zip 用于 ZIP 文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    相关资源
    最近更新 更多