In your HTML directly link to the file which is to be downloaded.
i.e.
a href="........./files/somefile.pdf">Download Pdf
for PHP
?php
// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_file'];
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$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;
// example: place this kind of link into the document where the file download is offered:
?
You will get a pop-up box asking whether you want to save the file or open it normally.
Now you want to download it so open .htaccess file and write there some lines according yo your requirement mentioning extension of files which you want to download directly.
Place the following code into your .htaccess file.
AddType application/octet-stream .pdf
AddType application/octet-stream .doc
AddType application/octet-stream .csv
This script will force download all files having extensions .pdf, .doc or .csv.
.htaccess files (or “distributed configuration files”) provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.
There is .htaccess file present by default at the root directory of your server. If not, you can always create a file named as .htaccess, code it and upload it in the desired directory or at the root level of your website.
In several web servers (most commonly Apache), .htaccess (hypertext access) is the default name of directory-level configuration files. A .htaccess file is placed in a particular directory, and the directives in the .htaccess file apply to that directory, and all subdirectories thereof. It provides the ability to customize configuration for requests to the particular directory. The file name starts with a dot because dot-files are by convention hidden files on Unix-like operating systems.
AddType application/vnd.ms-word.document.macroEnabled.12 .docm for docx in .htaccess in ubuntu
No comments:
Post a Comment