强制下载附件,而不是自动打开


Force download of attachment rather than opening it automatically

我有一个列出附件的网站。点击这些附件会导致两种行为之一——

  1. 附件在同一窗口中打开
  2. 该附件为用户提供了一个打开或保存文档的对话框

数字1似乎只发生在PDF中,但有没有一种方法可以让所有附件向用户显示保存/打开/取消弹出窗口?

仅适用于PDF。。

// The user will receive a PDF to download
header('Content-type: application/pdf');
// File will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The actual PDF file on the server is source.pdf
readfile('source.pdf');

对于所有其他文件类型,也许可以使用。。echo mime_content_type('Yourfile.ext') o

header('Content-type: '.mime_content_type('Yourfile.ext'));
header('Content-Disposition: attachment; filename="'.$output_filename.'"');
readfile($source_filename);

小心,我还没有测试过…

Content-type标头指定要下载的文件类型,由mime类型指定。Content Disposition标头为要下载的文件指定一个新文件名。readfile行不是发送的头,而是从文件中获取所有数据并输出的PHP调用。传递给readfile函数的参数是要下载的实际pdf文件的位置。

更新

不赞成使用mime_content_type()函数。你必须用这个来代替。。。

$finfo = new finfo;
$fileinfo = $finfo->file($file, FILEINFO_MIME);

您需要发送适当的内容类型headers()。

示例(适用于pdf,但如果您调整mimetype,则适用于任何内容):

<?php
header('Content-disposition: attachment; filename=huge_document.pdf');
header('Content-type: application/pdf');   // make sure this is the correct mime-type for the file
readfile('huge_document.pdf');
?> 

附加读数:

http://webdesign.about.com/od/php/ht/force_download.htm

使用@HappyApe友好提供的答案,以下是我在Wordpress中点击附件链接时用于强制下载文档的完整代码。

/**
 * Check that a page is the permalink of an attachment and force the download of the attahment if it is
 */
add_action('template_redirect', 'template_redirect');
function template_redirect(){
    global $post;
    /** Get the attachment URL as a pertty link ($url) and as a link to the file ($guid) */
    $url = get_permalink($post->ID);
    $guid = wp_get_attachment_url($post->ID);
    /** Get the file name of the attachment to output in the header */
    $file_name = basename(get_attached_file($post->ID));
    /** Get the location of the file on the server */
    $file_location = $_SERVER['DOCUMENT_ROOT'].substr($guid, strpos($guid, '/wp-content'));
    /** Get the file Mime Type */
    $finfo = new finfo;
    $mime_type = $finfo->file($file_location, FILEINFO_MIME);
    /** Check that the page we are on is a local attachment and force download if it is */
    if(is_local_attachment($url)) :
        header("Content-type: ".$mime_type, true, 200);
        header("Content-Disposition: attachment; filename=".$file_name);
        header("Pragma: no-cache");
        header("Expires: 0");
        readfile($guid); 
        exit();
    endif;
}

我一直将其用作通用mime,您所要做的就是传递一些参数:

// Check download token
if (empty($_GET['mime']) OR empty($_GET['token']))
{
    exit('Invalid download token 8{');
}
// Set operation params
$mime = filter_var($_GET['mime']);
$ext  = str_replace(array('/', 'x-'), '', strstr($mime, '/'));
$url  = base64_decode(filter_var($_GET['token']));
$name = urldecode($_GET['title']). '.' .$ext; 
// Fetch and serve
if ($url)
{
    $size=get_size($url);
    // Generate the server headers
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
    {
        header('Content-Type: "' . $mime . '"');
        header('Content-Disposition: attachment; filename="' . $name . '"');
        header('Expires: 0');
        header('Content-Length: '.$size);
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header("Content-Transfer-Encoding: binary");
        header('Pragma: public');
    }
    else
    {
        header('Content-Type: "' . $mime . '"');
        header('Content-Disposition: attachment; filename="' . $name . '"');
        header("Content-Transfer-Encoding: binary");
        header('Expires: 0');
        header('Content-Length: '.$size);
        header('Pragma: no-cache');
    }
    readfile($url);
    exit;
}
// Not found
exit('File not found 8{');

然后你通过这样的链接:

<a href="download.php?mime=++your file ext++&title=++ your file title ++&token=++your link to a file++">Download my file</a>

标题是可选的:)