2012-09-13 58 views
2

我有一个列出附件的网站。点击这些附件会导致两种行为之一 -强制下载附件而不是自动打开它

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

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

回答

3

对于只有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')Ø

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文件的位置。

UPDATE

mime_content_type()功能弃用。你会定义这个来代替......

$finfo = new finfo; 

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

非常感谢您的快速回复。这很好,我正要提及'mime_content_type()'被弃用,所以感谢更新。 –

+0

没有问题。很高兴帮助:) – TigerTiger

1

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

例(为PDF格式,但如果调整的MIME类型适用于任何东西):

<?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

+0

链接永远不能单独用作答案 - 请参阅http://meta.stackexchange。com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers/8259#8259 –

+0

Thaks @GungFoo。我认为这将是一个标题,但不知道如何。我接受了另一个答案,因为这也解释了如何获得我不知道该怎么做的'内容类型'。谢谢。 –

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; 

} 
0

我一直在使用这种通用MIME所有你需要做的就是通过一些PARAMS:

// 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> 

标题可选:)

相关问题