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");
Related
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.
I am handed over a PHP Code-igniter project by my Manager, and i have not a dependable experience in PHP. Im trying to download a newly created .csv file from server. But when i download it, it does not have the content of that file, instead it shows the header stript of my .html page where im doing the whole coding.
i am trying this using Force Downloading technique, mentioned all over internet.
$filename = $_SERVER['DOCUMENT_ROOT'] . '/apps/views/style/Default/files/'.'Attendance'.'_'.strtotime("now").'.csv';
$file = $filename;
if (is_file($file) == true) {
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header("Pragma: public", true);
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($file));
readfile($file);
}
This code runs on a button click, and the File does download, but it does not show the content, but when i manually download that same file directly from Cpanel server, it has content.
When i download it through this coding, it has the html scripts.
It is because you have the code inside a page, which already have html content displayed or in buffer to be displayed, you will have to implement your force download code inside a blank page or keep the code on top of page, so it give you download of the file content only.
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile($file);
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
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;
}
?>
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.