I want to require a file to be downloaded upon the user visiting a web page with PHP. I think it has something to do with file_get_contents
, but am not sure how to execute it.
$url = "http://example.com/go.exe";
After downloading a file with header(location)
it is not redirecting to another page. It just stops.
Display your file first and set its value into url.
index.php
download.php
In case you have to download a file with a size larger than the allowed memory limit (
memory_limit
ini setting), which would cause thePHP Fatal error: Allowed memory size of 5242880 bytes exhausted
error, you can do this:Read the docs about built-in PHP function readfile
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.
is correct
or better one for exe type of files
The following code is a correct way of implementing a download service in php as explained in the following tutorial
http://php.net/manual/en/function.readfile.php
That's all you need. "Monkey.gif" change to your file name. If you need to download from other server, "monkey.gif" change to "http://www.exsample.com/go.exe"