All TalkersCode Topics

Follow TalkersCode On Social Media

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

PHP Code For Downloading A File From Folder

Last Updated : Mar 11, 2024

PHP Code For Downloading A File From Folder

In this article we will show you the solution of PHP code for downloading a file from folder, in PHP the super global variable is a built-in variable that is always available in all scopes.

There are seven super global variables in PHP.

We used a super global variable $_GET to download files from a folder.

By using $_GET we can collect data from any html form.

Step By Step Guide On PHP Code For Downloading A File From Folder :-

At first, we saved an image named ‘image1.jpg’ that we are going to download within the same folder as the PHP file ‘download.php’.

Let us see the PHP code first.

<!DOCTYPE html>
<html lang = " en " >
<head>
    <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> PHP code for downloading a file from folder </title>
    <style>
        body {
            font-family : 'Lucida Sans', Verdana , sans-serif ;
        }
        h1 {
         font-size : larger ;
         font-weight : bolder ;
         color : rgb(113, 221, 113) ;
         text-align : center ;
        }
        h3 {
            text-align : center ;
        }
        div{
            font-weight: bolder ;
        }
    </style>
</head>
<body>
    <h1> TALKERSCODE </h1>
    <h3> PHP code for downloading a file from folder </h3>
    <div> CLICK HERE TO DOWNLAOD FILE: </div>
    <a href="download.php?file=image1.jpg"> CLICK!! </a>
</body>
</html>
<?php
if(!empty($_GET['file']))
{
 $filename = basename($_GET['file']) ;
 $filepath = 'destination/' . $filename ;
 if(!empty($filename) && file_exists($filepath)) {
        header ("Cache-Control: public") ;
  header ("Content-Description: FIle Transfer") ;
  header ("Content-Disposition: attachment; filename=$filename") ;
  header ("Content-Type: application/zip") ;
  header ("Content-Transfer-Emcoding: binary") ;
  readfile($filepath) ;
  exit ;
    }
    else {
        echo "file does not exist" ;
    }
}
?>
  1. First, we write <! DOCTYPE html> which we used as an 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 above now the <head> tag is used to contain 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. Created the <style> tag to add CSS to the HTML page.
  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.
  8. Now created a hyperlink with tag <a> with a href with the PHP file name and the image file name.
  9. Now we close the HTML file with </html> tag
  10. We write <?php tag to write PHP within it.
  11. In an ‘if’ statement if the file is not empty use the $_GET method
  12. Created a variable $filename then again use the $_GET to set the ‘basename’ as the file name
  13. Again create a variable $filepath to add the file path.
  14. Now again create an ‘if’ statement that if the filename is not empty and the file exists, then add some Headers within it.
  15. Using readfile() to download the file from the file path. Then exit.
  16. And also using an ‘else’ statement that if the file not exists then it returns a string that ‘file does not exist”.
  17. Now by clicking on the hyperlink we can download the file.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know to download a file from the folder using PHP.

I hope this article on PHP code for downloading a file from folder helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪