Posts Tagged ‘PHP’

Force file Download

No Comments »

Provide files to the user by forcing them to download.

<?php
    /***************************************
    *@file - physical path to file on server
    *@$filename4User - New file which will
    *    use as new name for downloaded file
    ****************************************/
    function force_download($file, $filename4User)
    {
        if ((isset($file))&&(file_exists($file))) {
           header("Content-length: ".filesize($file));
           header('Content-Type: application/octet-stream');
           header('Content-Disposition: attachment; filename="' . $filename4User. '"');
           readfile("$file");
        } else {
           echo "No file selected";
        }
    }
?>