Jun
21
2013

Force File Download with Correct Content Type

This function will force a specified file to download to the users computer using the path to the file to download, as well as the output filename.  This one is a ‘bit’ heftier, so I’ll add a download as a file as well just to be on the safe side.

/**
 * Force Download
 *
 * Generates headers that force a download to happen
 * Example usage:
 * force_download( 'screenshot.png', './images/screenshot.png' );
 *
 * @access public
 * @param string $filename
 * @param string $data
 * @return void
 */
function force_download( $filename = '', $data = '' )
{
    if( $filename == '' || $data == '' )
    {
        return false;
    }
    
    if( !file_exists( $data ) )
    {
        return false;
    }

    // Try to determine if the filename includes a file extension.
    // We need it in order to set the MIME type
    if( false === strpos( $filename, '.' ) )
    {
        return false;
    }

    // Grab the file extension
    $extension = strtolower( pathinfo( basename( $filename ), PATHINFO_EXTENSION ) );

    // our list of mime types
    $mime_types = array(

        'txt' => 'text/plain',
        'htm' => 'text/html',
        'html' => 'text/html',
        'php' => 'text/html',
        'css' => 'text/css',
        'js' => 'application/javascript',
        'json' => 'application/json',
        'xml' => 'application/xml',
        'swf' => 'application/x-shockwave-flash',
        'flv' => 'video/x-flv',

        // images
        'png' => 'image/png',
        'jpe' => 'image/jpeg',
        'jpeg' => 'image/jpeg',
        'jpg' => 'image/jpeg',
        'gif' => 'image/gif',
        'bmp' => 'image/bmp',
        'ico' => 'image/vnd.microsoft.icon',
        'tiff' => 'image/tiff',
        'tif' => 'image/tiff',
        'svg' => 'image/svg+xml',
        'svgz' => 'image/svg+xml',

        // archives
        'zip' => 'application/zip',
        'rar' => 'application/x-rar-compressed',
        'exe' => 'application/x-msdownload',
        'msi' => 'application/x-msdownload',
        'cab' => 'application/vnd.ms-cab-compressed',

        // audio/video
        'mp3' => 'audio/mpeg',
        'qt' => 'video/quicktime',
        'mov' => 'video/quicktime',

        // adobe
        'pdf' => 'application/pdf',
        'psd' => 'image/vnd.adobe.photoshop',
        'ai' => 'application/postscript',
        'eps' => 'application/postscript',
        'ps' => 'application/postscript',

        // ms office
        'doc' => 'application/msword',
        'rtf' => 'application/rtf',
        'xls' => 'application/vnd.ms-excel',
        'ppt' => 'application/vnd.ms-powerpoint',

        // open office
        'odt' => 'application/vnd.oasis.opendocument.text',
        'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
    );

    // Set a default mime if we can't find it
    if( !isset( $mime_types[$extension] ) )
    {
        $mime = 'application/octet-stream';
    }
    else
    {
        $mime = ( is_array( $mime_types[$extension] ) ) ? $mime_types[$extension][0] : $mime_types[$extension];
    }
        
    // Generate the server headers
    if( strstr( $_SERVER['HTTP_USER_AGENT'], "MSIE" ) )
    {
        header( 'Content-Type: "'.$mime.'"' );
        header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
        header( 'Expires: 0' );
        header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
        header( "Content-Transfer-Encoding: binary" );
        header( 'Pragma: public' );
        header( "Content-Length: ".filesize( $data ) );
    }
    else
    {
        header( "Pragma: public" );
        header( "Expires: 0" );
        header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
        header( "Cache-Control: private", false );
        header( "Content-Type: ".$mime, true, 200 );
        header( 'Content-Length: '.filesize( $data ) );
        header( 'Content-Disposition: attachment; filename='.$filename);
        header( "Content-Transfer-Encoding: binary" );
    }
    readfile( $data );
    exit;

} //End force_download

Get it as a file

[dm]15[/dm]

4 Comments + Add Comment

  • There is a slight problem in your code, where you get the Extension of the file you are sending to download, you are assuming that the file won’t have a . in it, other then for the extension itself, which is sadly incorrect….So a file named “Testing.File.txt”, will then try to download “Testing.File” from your server…

    The solution to this is to use pathinfo($filename, PATHINFO_EXTENSION); instead of explode( ‘.’, $filename );

    • You are correct, and the snippet has been updated accordingly!

      • Sorry my english is not realy good, 🙂 but if you change the explode to pathinfo you can use this on the validation extension i.e:

        $extension = strtolower( pathinfo( basename( $filename ), PATHINFO_EXTENSION ) );
        if( empty($extension) ) { return false; }

        Sorry for my english 🙁

  • Hope helps you!

    $mime_types=array(
    “pdf” => “application/pdf”,
    “txt” => “text/plain”,
    “html” => “text/html”,
    “htm” => “text/html”,
    “exe” => “application/octet-stream”,
    “zip” => “application/zip”,
    “doc” => “application/msword”,
    “xls” => “application/vnd.ms-excel”,
    “ppt” => “application/vnd.ms-powerpoint”,
    “gif” => “image/gif”,
    “png” => “image/png”,
    “jpeg”=> “image/jpg”,
    “jpg” => “image/jpg”,
    “php” => “text/plain”,
    “csv” => “text/csv”,
    “xlsx” => “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”,
    “pptx” => “application/vnd.openxmlformats-officedocument.presentationml.presentation”,
    “docx” => “application/vnd.openxmlformats-officedocument.wordprocessingml.document”
    );

    Source: http://w3webtools.com/simple-page-download-file-using-php-and-jquery/

Leave a comment