Here we are providing a simple code of explaining how to make a user to download file forcefully.
Clicking a link that points to a PDF or an Image file will not cause it to download to your hard drive directly. It will only open the file in your browser. Further you can save it to your hard drive. However, zip and exe files are downloaded automatically to the hard drive by default.
download.php
index.php
HTTP header fields
The Content-Length entity-header field indicates the size of the entity-body,
in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD
method, the size of the entity-body that would have been sent had the request
been a GET.
The text/html content type is an Internet Media Type as well as a MIME content type. Using HTML in MIME messages allows the full richness of Web pages to be available in e-mail. text/plain[RFC1521] The text/plain content type is the generic subtype for plain text.
In a regular HTTP response, the Content-Disposition response header is a header indicating if the content is expected to be displayed inline in the browser, that is, as a Web page or as part of a Web page, or as an attachment, that is downloaded and saved locally.
List of HTTP header fields
https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
Similarly, you can force download other files formats like word doc, pdf files, etc.
Normally, you don't necessarily need to use any server side scripting language like PHP to download images, zip files, pdf documents, exe files, etc. If such kind of file is stored in a public accessible folder, you can just create a hyperlink pointing to that file, and whenever a user click on the link, browser will automatically downloads that file.
<a href="demo.jpg">Download</a>
Clicking a link that points to a PDF or an Image file will not cause it to download to your hard drive directly. It will only open the file in your browser. Further you can save it to your hard drive. However, zip and exe files are downloaded automatically to the hard drive by default.
download.php
<?PHP
function force_download($file)
{
if ((isset($file))&&(file_exists($file))) {
header("Content-length: ".filesize($file));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $file . '"');
readfile("$file");
} else {
echo "No file selected";
}
}
index.php
<?php
include('download.php');
$file = 'demo.jpg';
force_download($file);
?>
HTTP header fields
The Content-Length entity-header field indicates the size of the entity-body,
in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD
method, the size of the entity-body that would have been sent had the request
been a GET.
The text/html content type is an Internet Media Type as well as a MIME content type. Using HTML in MIME messages allows the full richness of Web pages to be available in e-mail. text/plain[RFC1521] The text/plain content type is the generic subtype for plain text.
In a regular HTTP response, the Content-Disposition response header is a header indicating if the content is expected to be displayed inline in the browser, that is, as a Web page or as part of a Web page, or as an attachment, that is downloaded and saved locally.
List of HTTP header fields
https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
Similarly, you can force download other files formats like word doc, pdf files, etc.
https://norton-antivirus-support1.blogspot.com
ReplyDeletehttps://outlook-customer-support1.blogspot.com
https://kaspersky-antivirus-support.blogspot.com
https://avg-antivirus-support1.blogspot.com
https://avast-antivirus-support.blogspot.com
https://bitdefender-antivirus-support1.blogspot.com
https://hp-printer-support-1.blogspot.com
https://mcafee-antivirus-support1.blogspot.com
https://roku-support-number.blogspot.com
Thanks for sharing these type of useful information. keep it up.Digital signature for ICEGATE
ReplyDeleteNice your website and great information thanks.
ReplyDeleteKbc head office whatsapp number
This comment has been removed by the author.
ReplyDeleteSuper and great blog thanks admin for sharing such a great information
ReplyDeleteFood Beginning With Z
Nice blog thanks for sharing such a great information
ReplyDeleteHow Tall Are Bears