yuhyuhyuh
Otaku Community Builder
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
If you are looking for how to Force Download File in PHP then you are at the right place. If you are going to download any file using PHP, you need a browser to force download the file without displaying it. We are going to show you how to download any file direct to the server in PHP. The example script that can be used for downloading the file in any types like image, text, document, zip file and pdf.
Check the Live Demo.
⚙ Live Demo
PHP Download Query
This query use to download a file in PHP. Copy and paste this query and save it as "download.php".
Create Simple Markup and Download Button
This simple HTML source code where you can use the button to force download the file in PHP. Kindly copy and paste to the BODY tag of your web page.
Output
⚙ Live Demo
That's it, kindly click the "Download Code" button below for the full source code. Enjoy coding.
Download
Check the Live Demo.
⚙ Live Demo
PHP Download Query
This query use to download a file in PHP. Copy and paste this query and save it as "download.php".
- <?php
- if
(
!
empty
(
$_GET
[
'file'
]
)
)
{
- $fileName
=
basename
(
$_GET
[
'file'
]
)
;
- $filePath
=
'file_to_download/'
.
$downloadable_name
;
- if
(
!
empty
(
$fileName
)
&&
file_exists
(
$filePath
)
)
{
- // Define headers
- header
(
"Cache-Control: public"
)
;
- header
(
"Content-Description: File Transfer"
)
;
- header
(
"Content-Disposition: attachment; filename=$downloadable_name
"
)
;
- header
(
"Content-Type: application/zip"
)
;
- header
(
"Content-Transfer-Encoding: binary"
)
;
- // Read the file
- readfile
(
$filePath
)
;
- exit
;
- }
else
{
- echo
'The file does not exist.'
;
- }
- }
Create Simple Markup and Download Button
This simple HTML source code where you can use the button to force download the file in PHP. Kindly copy and paste to the BODY tag of your web page.
- <h1
>
- How to Force Download File in PHP
- </
h1
>
- <hr
/
>
- <br
/
>
- <a
href
=
"download.php?file=sourcecodester.png"
target
=
"_new"
>
- <button
class
=
"style_btn"
>
- Dowload File
- </
button
>
- </
a
>
- <br
/
>
- <br
/
>
- <br
/
>
- <hr
/
>
- <a
href
=
""
target
=
"_new"
class
=
"free_source_code"
>
- <h2
>
- Free Source Code
- </
h2
>
- </
a
>
Output

⚙ Live Demo
That's it, kindly click the "Download Code" button below for the full source code. Enjoy coding.
Download
You must upgrade your account or reply in the thread to view hidden text.