I've made a small program to search and display an PDF-file, some years ago. The PDF-file is shown inline, so the user can verify that it is the correct file. Usually he then uses the download button of the adobe plugin to save the file on his PC.
Since end of may this makes problems with Microsoft Edge and Google Chrome. The file is displayed correctly and when the user clicks the download button the save dialog opens, but when he presses the save button the file is not downloaded (error message "network error"). Strange thing is, that the print button next to the download button works.
On Firefox the whole thing still works. Each browser is updated automatically.
This is the code I use to display the pdf file:
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="' . $deliveryPdf->getFilename() . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
echo $deliveryPdf->getData();
Some notes, I recognized while analyzing the problem:
On Edge and Chrome the save dialog does not use the filename given in the response headers, while firefox uses the correct name
When using the "download" or "print" button in the adobe view, there is no additional request to the webserver (on none of the browsers). The only request on the webserver appears when initially opening the pdf
The print button works fine on all browsers. This raises the question where the difference between download and print is, when they both seem to use the cached file.
Related
I'm using this bit of code to download a file (path_facture_name) from the server to the client browser :
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($path_facture_name) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($path_facture_name));
ob_clean();
flush();
readfile($path_facture_name);
ob_end_flush();
# ----
# Some other PHP code
# ----
This works just fine, but when the file is downloaded, the script is ended, and the part Some other PHP code will never be executed.
So, my question is, is there a better way to download a file from the server that don't abort the execution of the next part of the code ?
I've tried to use <iframe> or JavaScript code to redirect the window to a sipparate .php file that will handle the download. But that didn't work because this feature that I wanna add is a part of an 18 years old complex php CRM that I can't easily/freely edit.
I'm looking for a PHP solution or guidelines.
Before submitting the form (or whatever takes you to the download page) use javascript to display sg. like "File downloading, press ok" and a button. It will stay because there's no HTML content to be loaded.
Set this button to load your download page again but skipping the file download headers this time (with a get switch).
The answer for this question is #ADyson's comment above :
« "and reload the current php page"...you can't do that, only the browser can do that in this situation. You've already given your response to the request in the form of a file download. The only way to "reload the current page" from the server would be to send some new HTML for the browser to display. But you can't respond to a single HTTP request with both a file download and a HTML document. It's physically impossible. You've already set headers indicating to the browser that the response is a file for download. »
So this is a HTTP limitation. We can't do a multi-part response.
I was unable to find an answer regarding headers. I have a .MP4 file hosted on website A and would like to force download using a button on website B. Is it possible without downloading the file to website B and then serving it to the user?
I have already pulled all the necessary data to website B, but in order to download a file, the user needs to right click to save on the button.
Here's what I use (PHP) where $cf contains the path to the file being requested. You may want a download script stored on server B that makes this happen and just link to the script to do the download .. http://link.to.script/script.php?file=thefilename.mp4'>Download I wouldn't normally put the filename in the URL but this is just an example.
header('Content-Disposition: attachment; filename="' . basename($cf) . '"');
header("Content-Length: " . filesize($cf));
header("Content-Type: application/octet-stream");
readfile(realpath($cf));
I am using php to show the pdf
#readfile($actualfilename);
header('Content-type:pdf');
header('Content-Disposition: inline');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($actualfilename));
header('Accept-Ranges: bytes');
the problem is on the tittle bar it shows
and when I use
header('Content-type:application/pdf');
it prompts me to download the file and the same problem appears when I use
header('Content-Disposition: inline; filename='.$fakefilename.'');
This code shows pdf file in only firefox. I.E,chrome prompts me to download file?
Are you trying to change the browser's title bar text (or wherever it shows the file name)?
If so, you're out of luck here, because that won't work as it's up to the browser to decide how/where (if at all) show the file name.
However, there's some possible workaround: You could use server side tools, such as mod_rewrite in an Apache environment to redirect a request like download/readme.pdf behind the scenes to readfile.php?file=readme.pdf. In this case the browser won't know about the hidden rewrite and it will in fact display readme.pdf as the file name (even if the real file name or the script's name on the server side are different).
I would like to display a PDF file as a result of browsing to the following address:
http://www.mydomain.com/myfile
To that end I am using a 301 Redirect on .htaccess which redirects myfile to myfile.php. This file then contains
<?php
$filename = 'files/somefile.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="somefile.pdf"');
header('Content-Length: ' . filesize($filename));
#readfile($filename);
?>
The main idea is that the PDF is displayed in the browser without prompting the user for download, but that when forcing the download the output filename becomes somefile.pdf. Everything is working smoothly in IE, Firefox, Chrome, Safari and Opera. However, in Android nothing happens. The PDF is not displayed and it doesn't prompt for download. How can I get this to work with Android? Is there anything I need to change in the script?
Android native browser doesn't have PDF support. You will need to embed the your url with google doc. Try this one..hope it will help..
url="http://docs.google.com/gview?embedded=true&url=http://118.102.182.50/victor/testpdf.aspx?s=9";
webView.loadUrl(url);
I have a simple PHP page for downloading files that contains this code.
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $name . '"');
readfile("$file");
This downloads the current bit of music I am listening to in a simple flash player.
For some reason in the latest Safari 4.0
When the download code is executed it interrupts the loading of the song to the flash player.
Basically it makes the flash player act as if it has reached the end of the file and loaded it all.
Kinda hacky, but you could embed a hidden iframe on the page and make the download link use that (href="download.php?filename=something" target="iframe_name"). Like I said, a bit lame, but it (theoretically) would do the trick since you're not redirecting the page technically.