How download file in php for big size?

by muriel.schmidt , in category: PHP General , 2 months ago

How download file in php for big size?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by wilmer.lemke , a month ago

@muriel.schmidt 

To download a big file in PHP, you can use the readfile() function. Here's an example:

1
2
3
4
5
6
7
8
$file = 'path/to/file.mp4'; // Replace with the path to your file
$filename = 'file.mp4'; // Replace with the desired filename for the downloaded file

header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: Binary');
header("Content-disposition: attachment; filename="".$filename.""");

readfile($file);


In the above code, you need to specify the path to the file you want to download using the $file variable, and you can specify the desired filename for the downloaded file using the $filename variable.


The headers set the content type to application/octet-stream to indicate that it's a binary file, and the Content-disposition header sets it as an attachment with the specified filename.


Finally, the readfile() function outputs the contents of the file directly to the browser. This allows you to download the file directly without loading it into memory all at once, making it efficient for big files.