How to download file from server using PHP?

If you want to make different types of files or images load the files directly into the drive of the user with PHP, you can run the readfile() function.

Let’s see how to do it on the example of creating an image gallery, which will help the users to download image files using just one click.

In the example below, an image-gallery.php is generated and a code is placed inside it:

So, in the example above, the download link points to the download.php file. The URL, on its turn, encompasses an image file name, just as a query string. Also, you can notice, that the urlencode() function is applied for encoding the image file names in a way that they may be safely passed like a URL parameter. The reason is that file names may include unsafe URL characters. The entire code of the download.php, forcing image download looks as follows:

Other file formats such as pdf, doc, and so on, can also be downloaded in the way, demonstrated above.

It is crucial to consider that in the example above, the regular expression (line 8) doesn’t allow files with the names that start or end with a dot (.). For example, you can use filenames such as books.jpg or Books.jpg but can’t use books.jpg., .kites.jpg, and so on.

In this post, we are going to see how to force download a file in PHP. Well, we can easily put an anchor link with the file path to be downloaded. Then why we are going to do it using PHP?

Using the normal anchor link with file path is publicly open and the file path can be found easily. But if we force download a file using PHP, then the file path will be hidden. So, for security reason sometimes PHP for download is necessary. But in those cases where there is no problem to let users know the file path, then it is okay to use the simple anchor link to let people download the file with that link. But in many cases, it is better to use PHP to let peoples download your file.

Generate PDF from HTML template in PHP using Dompdf

Upload File With Ajax Using PHP and jQuery

PHP code to forcefully download a file

Below is our PHP code to force download a file from the server:

<?php
$file_path = 'path_to_file/dummy_file.zip';
$filename = 'downloaded_file.zip';

header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$filename."\""); 
echo readfile($file_path);
?>

In the above code, first, we take the file path and give a new file name and then use PHP header to define the content type, content transfer, and content disposition. After that, we have used PHP readfile() function and send the file path as the parameter.

How to send email using PHP mail() function?

Now if we browse our PHP file that contains with the above PHP code and the file in that path then we will see that forcefully the file downloaded and the filename is as we defined in the variable within the code. In this case, we will see our downloaded file name is “downloaded_file.zip”.

Normally, you don't necessarily need to use any server side scripting language like PHP to download images, zip files, pdf documents, exe files, etc. If such kind of file is stored in a public accessible folder, you can just create a hyperlink pointing to that file, and whenever a user click on the link, browser will automatically downloads that file.

Download Zip file
Download PDF file
Download Image file
Download EXE file

Clicking a link that points to a PDF or an Image file will not cause it to download to your hard drive directly. It will only open the file in your browser. Further you can save it to your hard drive. However, zip and exe files are downloaded automatically to the hard drive by default.


Forcing a Download Using PHP

You can force images or other kind of files to download directly to the user's hard drive using the PHP readfile() function. Here we're going to create a simple image gallery that allows users to download the image files from the browser with a single mouse click.

Let's create a file named "image-gallery.php" and place the following code inside it.





Simple Image Gallery



    ';
            echo '';
            echo '

Download

'; echo '

'; } ?>

If you see the above example code carefully, you'll find the download link pints to a "download.php" file, the URL also contains image file name as a query string. Also, we've used PHP urlencode() function to encode the image file names so that it can be safely passed as URL parameter, because file names may contain URL unsafe characters.

Here's the complete code of "download.php" file, which force image download.

Similarly, you can force download other files formats like word doc, pdf files, etc.

The regular expression in the above example (line no-8) will simply not allow those files whose name starts or ends with a dot character (.), for example, it allows the file names such as kites.jpg or Kites.jpg, myscript.min.js but do not allow





Simple Image Gallery



    ';
            echo '';
            echo '

Download

'; echo '
0 or




Simple Image Gallery



    ';
            echo '';
            echo '

Download

'; echo '
1.

How to download a file from the server in PHP?

Initialize a file URL to the variable. Create cURL session. Declare a variable and store the directory name where the downloaded file will save. Use the basename() function to return the file basename if the file path is provided as a parameter.

How to download any file using PHP?

PHP enables you to download file easily using built-in readfile() function. The readfile() function reads a file and writes it to the output buffer..
<? ... .
header('Content-Type: application/octet-stream');.
header("Content-Transfer-Encoding: utf-8");.

How do I download files from another server?

The approach is as follows:.
Step 1: Login to server A using WinSCP..
Step 2: Download the files from server A to your local system (Windows).
Step 3: Login to server B using WinSCP..
Step 4: Upload the local files to server B..

How to download Excel file from server in PHP?

Export Data to Excel with PHP.
The $fileName variable defines the name of the excel file..
The Content-Disposition and Content-Type headers force the excel file to download..
Run the loop through each key/value pair in the $data array..
Display column names as the first row using the $flag variable..