2

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

  1. 附件将在同一窗口中打开。
  2. 附件向用户显示了一个对话框以打开或保存文档。

数字 1 似​​乎只发生在 PDF 上,但有没有办法让所有附件都向用户显示“保存/打开/取消”弹出窗口?

4

4 回答 4

4

仅适用于 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);
于 2012-09-13T10:46:44.993 回答
1

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

示例(对于 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

于 2012-09-13T10:37:10.200 回答
0

使用@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;

}
于 2012-09-13T11:47:06.813 回答
0

我一直将它用作通用 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>

标题是可选的:)

于 2015-09-11T22:54:35.533 回答