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 " > <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> file downloading in php </title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"> </head> <body> <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> </body> </html>
- First, we write <! DOCTYPE html> which we used as the 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 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.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Attach an external CSS file using <link> tag
- 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 and also adding the inline CSS here.
- 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.
- Close the link with </a> tag
- Close the HTML tag with </html>
PHP code:
<?php 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' ; } } ?>
- At first <?php tag to write the php.
- Creating an if statement with $_GET variable.
- Adding basename() with $_GET to get the $file_name and also adding the $file_path
- 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.
- Readfile() function added with the $file_path .
- Then use the exit statement to exit the loop.
- Now create an else statement to display the error message using echo.
- 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.