Generate download file link in PHP [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
open download dialog with php
I have a link in my page say, <a href='test.pdf'>(Test.pdf)</a>.
When I click on that link, download dialogue box should open to download that file.
Can anyone help me in implementing this in PHP?
thanks

$filename = 'Test.pdf'; // of course find the exact filename....
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false); // required for certain browsers
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'. basename($filename) . '";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filename));
readfile($filename);
exit;
Name the above file as download.php
HTML:
Test.pdf
That should do it.

test.pdf

In the case of a PDF file, most browsers are going to look for the helper (acrobat) to load it in your browser by default. You are trying to get around this default behavior is my guess.
The easiest way to do this (assuming you're on *nix box with apache) is to make an .htaccess file in the directory you want to have this result and add the line:
AddType application/octet-stream .pdf
This will cause any file with the extention .pdf to download by default. You can even have some .pdf files on the page load in the browser while others download by using the FilesMatch directive ( http://www.askapache.com/htaccess/using-filesmatch-and-files-in-htaccess.html ).
I realize your original question said "how do I do it with PHP" but I thought I'd post in case you were looking for a simpler, more elegant solution. Do keep in mind any directives you put in an .htaccess file will also affect any sub-directories below it.

Related

PHP file download not recognizing file type in Chrome and Edge

I'm trying to create a simple download page, which currently works in Firefox but not entirely in Chrome and Edge. The file to download is an mp3, located in a private directory on the server.
When I download the file in Firefox it works as intended. If I download it using Chrome or Edge the file still downloads, but it doesn't recognize the file type and just saves it as an extensionless file. If I add the .mp3 extension manually to the downloaded file it's fine and I can play the track.
These are the headers:
header("Cache-Control: private");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Length: " . filesize($filePath));
//Force download
readfile($filePath);
Changing the 'Content-type' to 'audio/mpeg' also doesn't work.
can you try:
header("Content-Disposition: attachment; filename=\"$title\".mp3");
to see if the title does not contain the extension

Allow access to protected file through PHP

I have a set of tar.gz files inside a folder protected against http access via .htaccess. I wish to allow direct curl downloading of these files via http. I have done this to things like images by setting the header information to something like:
$file='/some/file/protected/by/htaccess.png';
header("Content-type: image/png");
readfile($file);
My question:
Is there a way to provide direct access to these tar.gz files in a similar manner to the way I did it with images?
The reason I would like to do it in this way is that I would like to control access to which users can access these files by our site's login system.
Edit: as pointed out by Machavity, my code was pointing at a jpg, and had a png header.
For tar.gz
$filename = 'path/to/your/file.tar.gz';
header('Content-Type: application/x-compressed');
header("Cache-Control: no-store, no-cache");
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
header('Content-Length: ' . filesize($filename));
readfile($filename);

To download a csv file in specific folder automatically

I want to download a csv file using a link automatically in a specified folder by running a cron job using PHP.
I tried the below code ,but it gets downloaded in download folder by default.
can any one show me the right way to do it.The code used is below.
$link="http://labcase.com/wrt/search.php?format=csv&sortby=reqnum|DESC&Search=non_closed_req&state[]=New&state[]=Pending%3A+Delivery&state[]=Pending%3A+Installation&state[]=On+Hold&state[]=Pending%3A+More+Info&state[]=Assigned&state[]=Working&state[]=Pending%3A+Approval&orgs[]=125&orgs[]=25&bldg[]=BGL04%2CBGL11%2CBGL12%2CBGL13%2CBGL14%2CBGL15%2CBGL16%2CBGL17%2CBGL20%2CBGL22%2CBGL25%2CBGL26%2CBGL43&business_unit=all";
header('Content-Disposition: attachment; filename=example.csv');
header("Content-Type: application/force-download");
header('Pragma: no-cache');
readfile($link);
You can use this:
file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
Take a look here

How To Download File TO Desired FOLDER/Path?

I am having a problem with how to download the file into our desired folder, this below is my code for download.php
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fileName));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header ("Cache-Control: must-revalidate");
header('Pragma: public');
ob_clean();
flush();
readfile($data);
echo $data;
exit;
I want to download the file to my desired folder like C:\Users\Asus or D:\Program not to the default one
$data is the content of the file and $filename already include the extension Ex:picture.jpg
Thx before for all the help that i can get :)
Henry
There is no way to tell the client where on the client's disk to save a file to.
You have no way of knowing (on the WWW at least) what directories exist
If the user doesn't pay attention to where something is being saved (because they expect it to be the default) then you could save the file somewhere they wouldn't want it (such as a Startup folder) which would be a security risk.
The download directory is usually handled by the browser, not by the server or PHP it's self :)

Copy and download file in htaccess protected folder

I have a htaccess password protected folder with several files in it. Users are not allowed to access all files, but are allowed to download their own.
Since i can't direct link the file and since copying / removing isn't a real solution, i thought i'd just open the file using file_get_contents and echo it back into the page using the right header. But.. i don't get it working.. Here is my code. The error i am getting is that when opening the file i get a "file is damaged" error from Acrobat.
<?php
$file = "cms/docs/5641-1.pdf";
header('Content-Description: File Transfer');
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename='.basename("exoticfilename.pdf"));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
if (file_exists($file))
{
echo file_get_contents($file);
}
?>
Also, in this example I am just using a PDF file, but there are several types of files. Therefore i should probably change the header depending on the file type. Is there a solution for that, or should i just use a very long if / else statement?
If there is another, better way, I am open for that.
UPDATE
The above works, but not with all files. Older PDF's (Acrobat 6) don't work, but Acrobat X files do. Same counts for the docx files. Some work, others don't. Very weird, since I am able to open all directly on my PC. I assume it has something to do with the application/pdf line (or application/vnd.openxmlformats-officedocument.wordprocessingml.document' for docx). All others, like images, work.
Since you are using htaccess/htpasswd to protect the directory from hot-linking leeches. You are inadvertanly blocking access to the files from an outside source such as a browser from the client side. Since the directory requires authentication to access the files within it, you need to script around it. In a sense authenticating through the script. I have seen it done before, and you can find one of many references on the subject here
http://koivi.com/php-http-auth/
but bottom line is htaccess and htpasswd over run your scripts even if on the same host machine, as they are in a lack of better description server level, ran before even php starts its process on a page load.

Categories