75 PHP Upload & Download Files sorted by best sellers. Customer login, distribute files, download manager, ftp, nodatabase, php, script See all tags.
Active4 months ago
![File File](https://itsolutionstuff.com/upload/PHP-Angular-JS.png)
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.After downloading a file with
Peter Mortensenheader(location)
it is not redirecting to another page. It just stops.14.5k19 gold badges89 silver badges118 bronze badges
johnjohn
10 Answers
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.
Php Download Zip File Script
Victor Bocharsky6,9856 gold badges42 silver badges79 bronze badges
Pit DiggerPit Digger6,20118 gold badges64 silver badges116 bronze badges
shookees2822 gold badges6 silver badges18 bronze badges
genesisgenesis44.6k15 gold badges86 silver badges114 bronze badges
Or, when the file is not openable with the browser, you can just use the
Marek SeberaMarek SeberaLocation
header:27k32 gold badges143 silver badges220 bronze badges
Display your file first and set its value into url.
index.php
download.php
hemnath mouli2,0912 gold badges10 silver badges24 bronze badges
jundell agbojundell agbo
In case you have to download a file with a size larger than the allowed memory limit (
Parziphalmemory_limit
ini setting), which would cause the PHP Fatal error: Allowed memory size of 5242880 bytes exhausted
error, you can do this:Php Script Download File From Ftp
Parziphal3,4742 gold badges24 silver badges27 bronze badges
A modification of the accepted answer above, which also detects the MIME type in runtime: Dragon ball ep 6.
Jure SahJure Sah
The following code is a correct way of implementing a download service in php as explained in the following tutorial
Grigor NazaryanGrigor Nazaryan
Zevan8,2153 gold badges24 silver badges39 bronze badges
nqobilenqobile
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'
AssasinAssasin
You can stream download too which will consume significantly less resource.example:
In the above example, I am downloading a test.zip (which is actually the android studio zip on my local machine).php://output is a write-only stream (generally used by echo or print).after that, you just need to set the required headers and call stream_copy_to_stream(source, destination).stream_copy_to_stream() method acts as a pipe which takes the input from the source stream (read stream) and pipes it to the destination stream (write stream).
Saud QureshiSaud Qureshi
Not the answer you're looking for? Browse other questions tagged phpfiledownload or ask your own question.
if you need to limit download rate, use this code
<?php
$local_file = 'file.zip';
$download_file = 'name.zip';
// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5;
if(file_exists($local_file) && is_file($local_file))
{
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($local_file));
header('Content-Disposition: filename='.$download_file);
flush();
$file = fopen($local_file, 'r');
while(!feof($file))
{
// send the current file part to the browser
print fread($file, round($download_rate * 1024));
// flush the content to the browser
flush();
// sleep one second
sleep(1);
}
fclose($file);}
else {
die('Error: The file '.$local_file.' does not exist!');
}
?>