Home »
PHP »
PHP programs
PHP program to download the text file from the specified URL
Here, we are going to learn how to download the text file from the specified URL in PHP?
Submitted by Nidhi, on November 22, 2020 [Last updated : March 14, 2023]
Downloading Text File from Given URL
Here, we will demonstrate how we can download a text file from a specified URL using the PHP program.
PHP code to download the text file from the specified URL
The source code to download the text file from the specified URL is given below. The given program is compiled and executed successfully.
<?php
//PHP program to download the text file.
$download_url = 'http://www.includehelp.com/sample.txt';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: utf-8");
header("Content-disposition: attachment; filename=\"" . basename($download_url) . "\"");
readfile($download_url);
?>
Explanation
Here, we created a variable $download_url that contains the file URL that will be downloaded. After that, we prepared the header and call readfile() to download the file.
More PHP Programs »