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" ; } } ?>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- 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.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Created the <style> tag to add CSS to the HTML page.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here.
- Now created a hyperlink with tag <a> with a href with the PHP file name and the image file name.
- Now we close the HTML file with </html> tag
- We write <?php tag to write PHP within it.
- In an ‘if’ statement if the file is not empty use the $_GET method
- Created a variable $filename then again use the $_GET to set the ‘basename’ as the file name
- Again create a variable $filepath to add the file path.
- Now again create an ‘if’ statement that if the filename is not empty and the file exists, then add some Headers within it.
- Using readfile() to download the file from the file path. Then exit.
- And also using an ‘else’ statement that if the file not exists then it returns a string that ‘file does not exist”.
- 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.