downloading pdf files using php [duplicate] - php

This question already has answers here:
PHP output file on disk to browser
(6 answers)
PHP: How to make browser to download file on click
(2 answers)
Closed 5 years ago.
I have PHP files stored on my server, and their names in the mysql database, I want to download those files. What code should I write for the same? I am using PHP as coding language. Please help.

<?php
$file = 'send_me.pdf';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
Obviously, set $file to the file name.
Read more about the use of readfile here.

Download?
Literally just make a link the stored file.

file_put_contents("PDFName.pdf", fopen("http://someurl/PDFName.pdf", 'r'));
You really should show what you have done so far/researched online before asking a question!
This will download the file PDFName.pdf from the url http://someurl/PDFName.pdf and put it into the same directory as the script is in.

Related

Viewing a file when clicked not downloaded? [duplicate]

This question already has answers here:
What is correct content-type for excel files? [duplicate]
(3 answers)
Closed 3 years ago.
When I do a Google search I find a TON of replies about the code needed to download a file when clicked and that works just fine. What I am looking for is when I click on the link it will just open up the file in the browser instead of prompting to download the file. Here is the working code to Download a file:
$file = $invoice_dir . '/test.xlsx';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
How do I just make it open up instead using my local software? It is an XLSX Office type file not plain text. I tried include() and it outputs jibberish.
You just needed to open the file in a href like this
<a href"http://www.yourdomain.com/index.php?file=file.pdf">View File</a>
and in case you want to open it in a new browser tab then
<a href"http://www.yourdomain.com/index.php?file=file.pdf" target="_blank">show file</a>
Add this code
header("Content-type: application/vnd-ms-excel");

PHP - Forcing an MP3 file download

So, I need a little help here. I have a site which hosts some mp3s. When users click on the download url, it links directly to a file called downloadmp3.php, which goes 2 parameters in the url...the php file is included below, and it's basically supposed to FORCE the user to save the mp3. (not play it in the browser or anything).
That doesnt happen. Instead, it seems like the file is WRITTEN out in ascii to the browser. It seems like it's the actual mp3 file written out.
Here is my downloadmp3.php file...please, what's wrong in this code.
It works on my local LAMP (Bitnami Wampstack on windows)....that is, on my local testing environment, it sends the file to my broswer, and I can save it. When I upload it to the real server, it basically writes out the mp3 file.
Here is the culprit file, downloadmp3.php...please help
<?php
include 'ngp.php';
$file = $_GET['songurl'];
$songid = $_GET['songid'];
increasedownloadcount($songid);
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: audio/mpeg');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
ob_clean();
flush();
readfile($file);
exit;
}
?>
By the way, this site only hosts mp3s - no other audio or file format. So, this downloadmp3.php script should ideally ask the user where they want to save this file.
Thanks for your help in advance.
I think the filename should be in quotes:
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
Change the content-type value to text/plain. With this browser wont recognize it and wont play the file. Instead it will download the file at clients machine.
Seems there is too many headers. I am sure they do SOMETHING... but this code works.
This code works with MP3 files.... downloads to a file. Plays without a problem.
if(isset($_GET['file'])){
$file = $_GET['file'];
header('Content-type: audio/mpeg');
header('Content-Disposition: attachment; filename=".$file.'"');
readfile('path/to/your/'.$file);
exit();
}
You can access it with ajax call, or this:
<a id="dl_link" href="download.php?file=<>file-you-wish-to-download<>" target="_blank">Download this file</a>
Hopefully this is of some use

Download file to website using php [duplicate]

This question already has answers here:
Download File to server from URL
(12 answers)
Closed 8 years ago.
I have a file with URL !
$url = "http://www.example.com/aa.txt";
and I want to download this file and save it to path on my website
this is my website ( online )
$path = "server/username/";
i want the $url file saved to $path ,,
i try this
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
when I test the code , it make the file download to my computer not to my website
you can use file_put_contents();
see manual here:http://php.net/manual/en/function.file-put-contents.php
a different implementation here:http://www.finalwebsites.com/forums/topic/php-file-download

Size not showing while downloading files from my site [duplicate]

This question already has answers here:
Size not showing while downloading files from my website
(3 answers)
Closed 8 years ago.
Please see the screenshot below. When someone is downloading files from my site it, while downloading size is not showing. Thus, users cannot know how much is remaining, what is the size. Can someone help me or tell any idea to fix this? Is this a problem with the server? Or I have to put any code or something?
You should properly send the headers from your server
Better look at this :- http://php.net/manual/en/function.readfile.php
Example
<?php
$file = 'yourfile.apk';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>

Force user to download file in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Forcing to download a file using PHP
When we need to force user to download a file, we use header with several parameters/options. What if I use
header("location:test.xlsx");
This is working :) Is there any drawback of using this shortcut ?
This approach should solve the problems mentioned here
download.php?filename=test.xlsx
if isset ($_GET['filename']){
$filename = $_GET['filename']
}
else{
die();
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
And of course don't forget to secure this so users can't download other files
There are a few disadvantages to this method:
If the file is one the browser can read, it won't be downloaded (like .txt, .pdf, .html, .jpg, .png, .gif and more), but simply be shown within the browser
Users get the direct link to the file. Quite often, you don't want this because they can give this link to others, so...
it will cost you more bandwidth
it can't be used for private files
if it's an image, they can hotlink to it
All you're doing is redirecting to a file. This is no different than if they went to it directly.
If you are trying to force a download, you need to set your Content-Disposition header appropriately.
header('Content-Disposition: attachment');
Note that you can't use this header when redirecting... this header must be sent with the file contents. See also: https://stackoverflow.com/a/3719029/362536
Not every file is forced to download.
If you were to use that header() on a .jpg the browser won't open the download dialog but will just show the image.

Categories