I am working with a friend to help develop a simple interface for a backup system he is operating. Basically, the script displays a secure list of files by filename and each filename is clickable to download the corresponding file. I have forced the download of files using PHP in the past but the method I used required knowledge of the files type. With this project that method is not feasible as this system will have a wide array of possible file types.
I compiled some resources I found online to create the following simple script that forces the download of any file type with PHP: (Note I have not actually tested it with every possible file type but the ones I did used worked great)
<?PHP //BUILD THE FILE INFORMATION $file = $filepath . $filename; //CREATE/OUTPUT THE HEADER header("Content-type: application/force-download"); header("Content-Transfer-Encoding: Binary"); header("Content-length: ".filesize($file)); header("Content-disposition: attachment; filename=\"".basename($file)."\""); readfile($file); ?>