All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

File Downloading In PHP

Last Updated : Mar 11, 2024

File Downloading In PHP

In this article we will show you the solution of file downloading in PHP, we force download a file. The file can be in any format as pdf, jpg, etc.

If the file is present in the mentioned location we can download it by clicking on the HTML link.

We will see an example of downloading an image file below.

Step By Step Guide On File Downloading In PHP :-

<!DOCTYPE html>
<html lang = " en " >
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> file downloading in php </title>
    <link rel="stylesheet" href="">
    <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h2> file downloading in php </h2>
        <a href="download.php?file=tree.jpg"> Download File </a>
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Attach an external CSS file using <link> tag
  6. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  7. <h1> tag used to add heading here and also adding the inline CSS here.
  8. Adding an HTML link tag using < a > tag with the attribute of ‘href’. Into that href add the name of the php file. Here we named the php file download.php. within it add the file name of the file we are going to download.
  9. Close the link with </a> tag
  10. Close the HTML tag with </html>

PHP code:

if(!empty ($_GET ['file'])) {
    $file_name = basename($_GET ['file']) ;
    $file_path = 'destination/'. $file_name ;
    if(!empty ($file_name) && file_exists ($file_path)) {
        header ("Cache-Control: public") ;
        header ("Content-Description: File Transfer") ;
        header ("Content-Disposition: attachment; filename=$file_name") ;
        header ("Content-Type: application/zip") ;
        header ("Content-Transfer-Encoding: Binary") ;
        readfile($file_path) ;
        exit ;
    } else {
        echo 'the file dost not exist' ;
  1. At first <?php tag to write the php.
  2. Creating an if statement with $_GET variable.
  3. Adding basename() with $_GET to get the $file_name and also adding the $file_path
  4. Now create another if tag within it and add a condition if the file name is not empty and the file path exists. Then adding some block-off headers to it.
  5. Readfile() function added with the $file_path .
  6. Then use the exit statement to exit the loop.
  7. Now create an else statement to display the error message using echo.
  8. Close the php code with ?> tag

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to download files using PHP.

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

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪