How to force file download with PHP How to force file download with PHP php php

How to force file download with PHP


Read the docs about built-in PHP function readfile

$file_url = 'http://www.myremoteserver.com/file.exe';header('Content-Type: application/octet-stream');header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); readfile($file_url); 

Also make sure to add proper content type based on your file application/zip, application/pdf etc. - but only if you do not want to trigger the save-as dialog.


<?php$file = "http://example.com/go.exe"; header("Content-Description: File Transfer"); header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"". basename($file) ."\""); readfile ($file);exit(); ?>

Or, when the file is not openable with the browser, you can just use the Location header:

<?php header("Location: http://example.com/go.exe"); ?>


header("Content-Type: application/octet-stream");header("Content-Transfer-Encoding: Binary");header("Content-disposition: attachment; filename=\"file.exe\""); echo readfile($url);

is correct

or better one for exe type of files

header("Location: $url");