How to automatically start a download in PHP ?
This post deals with creating a start downloading file using PHP. The idea is to make a download button which will redirect you to another page with the PHP script that will automatically start the download.
Creating a download button:
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML course.
To redirect to some file which has the file to be downloaded, create an HTML form as shown below.
PHP code to download: When the user clicks the above button, the code will be redirected to the “downloadFile.php” file. Now, use the URL of the file and PHP file_get_contents() function to download the file.
- Before running the program:
- After running the program: