All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

File Download Code In PHP With Demo

Last Updated : Mar 11, 2024

File Download Code In PHP With Demo

In this article we will show you the solution of file download code in PHP with demo, a hyperlink in your browser can usually be used to open a file. If this situation arises, you can manually download a file from your browser.

To download the file dynamically and save it to the local drive automatically, force the browser to save it instead of displaying it.

By using PHP, users can force file downloads, which means the requested files are downloaded without rendering in the browser.

PHP provides the header () and readfile () functions for downloading files. Use the following code example to demonstrate how to force download files in PHP.

The PHP script also allows one to create a download link for downloading files from a directory. Any type of file can be uploaded using the following example script, including text, images, documents, pdfs, zips, etc.

Downloading files with the extensions exe and zip does not generally require PHP scripts. A user clicks the download link to open them in the browser instead.

Step By Step Guide On File Download Code In PHP With Demo :-

<?php
         // Initialize a file URL to the variable
         $url =
         'http://talkerscode.com/php/php.php';
         // Use basename() function to return the base name of file
         $file_name = basename($url);
         // Use file_get_contents() function to get the file
         // from URL and use file_put_contents() function to
         // save the file by using the base name
         if (file_put_contents($file_name, file_get_contents($url)))
         {
                     echo "File downloaded successfully";
         }
         else
         {
                     echo "File downloading failed.";
         }
?>
  1. The short tags start with "<?" and end with "?>".
  2. Then we use the URL for creating and setting the link.
  3. A file's base name is returned by the basename() function if the path is provided by the file's path parameter.A file's base name is returned by the basename() function if the path is provided by the file's path parameter.
  4. Then we use the if and else statements to create an echo statement.

Conclusion :-

Normally, a hyperlink can be used to open a file in the browser. In such a case, the file can be manually downloaded from the browser.

If you want to download the file dynamically and save it on the local drive automatically, force the browser to download the file instead of displaying it.

I hope this article on file download code in PHP with demo helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪