How to show pdf file detail using php - php

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).

Related

PHP Problem with saving of inline pdf in Chrome and Edge

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.

How to force download of an .mp4 file by PHP headers from external url?

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));

How to disable downloading a file from your web without a session variable?

I don't actually now hoy to ask this question, so it may be probably repeated. Let's see: I would like to disable downloading a file from my web without a download script (just using the URL: http://something/file.zip) unless you're registered, with PHP preferably. Yes, it's a very common topic but I haven't found any information! A lot of pages do this, such as uploaded.net. I hope you understand what I'm talking about. Thanks!
First and foremost, don't allow direct access to the file. Store it outside of your web application's root folder, elsewhere on the file system, so that there is no link which can be used to download it. This is because direct access skips any PHP application and interacts only with the web server, which has no knowledge of your application's session values.
Then create a "download" script to serve the file to users. Generally such a script would be given some identifier for the file, something like:
http://yourserver.com/download.php?file=file.zip
(Important: Be very careful how you identify that file. Do not just blindly let users download whatever they want, or they can enter longer paths onto the URL and download any file from your server. Always validate access to things first.)
This would be just like any other PHP script, except that instead of displaying HTML it would return a file. The actual part of outputting the file can be as simple as:
readfile('/path/to/file.zip');
You'd also likely want to set content headers appropriately, etc. A more complete example can be found in the documentation:
<?php
$file = 'monkey.gif';
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;
}
?>

PHP pdf file opened in chrome browser

I have a script that merges 2 documents and then it shows a final document in a browser.
It works in Firefox, Opera and IE. But it does not work in Chrome.
Chrome only shows loading and it stops in 1/4 of loading.
The code:
exec("pdftk A=$pdfin B=$tmpfname cat B1 A output $tmpfoutput");
$data = file_get_contents($tmpfoutput);
header("Content-type: application/pdf");
header("Content-disposition: inline;filename=GeneratedPdf.pdf");
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($tmpfoutput));
header('Accept-Ranges: bytes');
echo $data;
I have been told that it also worked in Chrome before so I think there will be only a problem with the last version.
Thanks for any help.
By appending Accept-Ranges: bytes, your script tells the browser that it will accept range requests, i.e. multiple requests that request a part of the response. Your script obviously does not support range requests because it generates and provides the data at once.
To fix the error, remove header('Accept-Ranges: bytes');
If your PDF files are usually large, then a more user-friendly solution is to actually implement range requests in your script. Odds are that your server does already have an efficient routine that handles range requests, so a smart choice is to save PDF file to a publicly accessible directory, then 302-redirect the request to this URL after the PDF has been generated. Make sure that the URLs are unguessable, e.g. by using UUIDs. And remove the PDF files at some point, e.g. using a cronjob.

How to use the CSV MIME-type?

In a web application I am working on, the user can click on a link to a CSV file. There is no header set for the mime-type, so the browser just renders it as text. I would like for this file to be sent as a .csv file, so the user can directly open it with calc, excel, gnumeric, etc.
header('Content-Type: text/csv');
echo "cell 1, cell 2";
This code works as expected on my computer (Isn't that how it always is?) but does not work on another computer.
My browser is a nightly build of FF 3.0.1 (on linux). The browsers it did not work in were IE 7 and FF 3.0 (on windows)
Are there any quirks I am unaware of?
You could try to force the browser to open a "Save As..." dialog by doing something like:
header('Content-type: text/csv');
header('Content-disposition: attachment;filename=MyVerySpecial.csv');
echo "cell 1, cell 2";
Which should work across most major browsers.
You are not specifying a language or framework, but the following header is used for file downloads:
"Content-Disposition: attachment; filename=abc.csv"
With Internet Explorer you often have to specify the Pragma: public header as well for the download to function properly..
header('Pragma: public');
Just my 2 cents..
This code can be used to export any file, including csv
// application/octet-stream tells the browser not to try to interpret the file
header('Content-type: application/octet-stream');
header('Content-Length: ' . filesize($data));
header('Content-Disposition: attachment; filename="export.csv"');
I tried to use text/csv but it did not work for me after that tried different stuff and I figured out that if we use this text/plain. Now the file upload is completed. as expected. This problem was in the Yii2 file upload widget.
Example response after success.
{"files":[{"name":"unit_ids list - Sheet1.csv","type":"text/plain","size":30,"base_url":"https://s3-eu-west-1.amazonaws.com/cdn.abc.co","path":"1/g2qVy3JtyZBLaRUd8c5gMOtSyrTEwdzR.csv","url":"https://s3-eu-west-1.amazonaws.com/cdn.abc.co/1/g2qVy3JtyZBLaRUd8c5gMOtSyrTEwdzR.csv","delete_url":"/coupons/default/sheet-delete?path=1%2Fg2qVy3JtyZBLaRUd8c5gMOtSyrTEwdzR.csv"}]}

Categories