Mar
29
2017
Force Download Files in PHP
One of the most common problems that we find is to force the download of a certain file from the client’s browser, thus preventing it from opening.
For this we can use the following function in PHP.
function force_download($file) { if ((isset($file))&&(file_exists($file))) { header("Content-length: ".filesize($file)); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $file . '"'); readfile("$file"); } else { echo "No selected file"; } }