TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to Download File using PHP
Bhushan Singh
Oct 15
2014
Code
1.9
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
download.zip
?php
if
(isset(
$_GET
[
'download_file_name'
])){
$mypath
=
$_SERVER
[
'DOCUMENT_ROOT'
].
"/download/"
;
// change the path to fit your websites document structure
$file_location
=
$mypath
.
$_GET
[
'download_file_name'
];
if
(
$fd
=
fopen
(
$file_location
,
"r"
)) {
$fsize
=
filesize
(
$file_location
);
$path_parts
=
pathinfo
(
$file_location
);
$ext
=
strtolower
(
$path_parts
[
"extension"
]);
switch
(
$ext
) {
case
"pdf"
:
header(
"Content-type: application/pdf"
);
// add here more headers for diff. extensions
header(
"Content-Disposition: attachment; filename=\""
.
$path_parts
[
"basename"
].
"\""
);
// use 'attachment' to force a download
break
;
default
;
header(
"Content-type: application/octet-stream"
);
header(
"Content-Disposition: filename=\""
.
$path_parts
[
"basename"
].
"\""
);
}
header(
"Content-length: $fsize"
);
header(
"Cache-control: private"
);
//use this to open files directly
while
(!
feof
(
$fd
)) {
$buffer
=
fread
(
$fd
, 2048);
echo
$buffer
;
}
}
fclose (
$fd
);
exit
;
}
?>
<a href=
"download.php?download_file_name=dum.pdf"
>Download here</a>
download file php
PHP