How can i download php file code?

Normally, a hyperlink can be used to open a file on the browser. In such a case, the file can download from the browser manually. 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. Force file download functionality allows the user to download files in PHP where the requested files are downloaded forcefully without rendering on the browser. In this tutorial, we are going to show how to download a file from a directory or server in PHP.

Using the header() and readfile() function, you can easily download a file in PHP. Here we’ll provide the example PHP code to force download file in PHP. Also, this simple PHP script helps to implement a download link that downloads a file from the directory. The following example script can be used to download any type of file like text, image, document, pdf, zip, etc.

Download File in PHP

<?php 
// Define file name and path
$fileName 'Brochure.pdf';
$filePath 'files/'.$fileName;

if(!empty(

$fileName) && file_exists($filePath)){
    
// Define headers
    
header("Cache-Control: public");
    
header("Content-Description: File Transfer");
    
header("Content-Disposition: attachment; filename=$fileName");
    
header("Content-Type: application/zip");
    
header("Content-Transfer-Encoding: binary"); // Read the file
    
readfile($filePath);
    exit;
}else{
    echo 
'The file does not exist.';
}

In the web application, you need to provide a hyperlink to allow the user to download files from the server dynamically. Use the below sample code to display an HTML link to download a file from the directory using PHP.
HTML Code:

<a href="download.php?file=Brochure.pdf">Download File</a>

PHP Code (download.php):

<?php if(!empty($_GET['file'])){ 
    
// Define file name and path
    
$fileName basename($_GET['file']);
    
$filePath 'files/'.$fileName;

    if(!empty(

$fileName) && file_exists($filePath)){
        
// Define headers
        
header("Cache-Control: public");
        
header("Content-Description: File Transfer");
        
header("Content-Disposition: attachment; filename=$fileName");
        
header("Content-Type: application/zip");
        
header("Content-Transfer-Encoding: binary"); // Read the file
        
readfile($filePath);
        exit;
    }else{
        echo 
'The file does not exist.';
    }
}
?>

Force Download File from Remote Server in PHP

Are you want to get implementation help, or modify or enhance the functionality of this script? Submit Paid Service Request

If you have any questions about this script, submit it to our QA community - Ask Question

Where can I find PHP code?

A PHP script can be placed anywhere in the document. The default file extension for PHP files is " .php ". A PHP file normally contains HTML tags, and some PHP scripting code.

How do I save a PHP script?

Save the File.
Choose Save from the File menu..
Enter your_file_name. php into the Save As field, being sure to include the . php extension..
Click the Save button..

How do I open a downloaded PHP file?

Go to the location in which your PHP file is stored, then click the PHP file to select it. Click Open. It's in the bottom-right corner of the window. Doing so opens the PHP file in BBEdit; you should now be able to see the PHP file's text.

How do I save a PHP file in Notepad ++?

This is done by navigating to File > Save As... In Notepad, add . php to the end of the filename and enclose in double quotations. This ensures the file will not be converted into a basic text file by Notepad.