Force download of attachment rather than opening it automatically Force download of attachment rather than opening it automatically wordpress wordpress

Force download of attachment rather than opening it automatically


For the PDF only ..

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

...

for all other file type perhaps you could use .. 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);

Beware that I haven't tested it...

The Content-type header specifies what type of file is to be downloaded, specified by a mime type.The Content-Disposition header specifies a new filename for the file which is to be downloaded.The readfile line is not a header being sent, but a PHP call that gets all the data from a file and outputs it. The argument you pass to the readfile function is the location of the actual pdf file to be downloaded.

UPDATE

mime_content_type() function is deprecated. you'll ned to replace with this ...

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


you need to send the appropriate content-type headers().

Example (for pdf, but works for anything if you adjust the mimetype):

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

Additional reading:

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


Using the answer kindly provided by @HappyApe, here is the full code that I use to achieve forcing download of a document when clicking on an attachment link in 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;}