PDF file won't display - php

So, I've followed a lot of posts here on StackOverflow still can't work out why my code doesn't work.
I want to be able to open a .pdf file outside the web root, so I've tried this code:
<?php
$file = '/user/Desktop/exemplo.pdf';
echo $file.'<br>';
$filename = 'test.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
#readfile($file);
?>
Problem is I always get the same error:
pdf this file cannot be correctly displayed
I've decided to try several browsers: firefox, chrome, opera and finally internet explorer which suprinsingly said something the others didn't:
file does not start with '%pdf-'Local\EWH-696-6
So does someone know what I need to configure in my code to open pdf's? Or to have them start with the code above (hardcoding it didn't fix it).
EDIT: tried removing the echo and still had the same error.

Related

Displaying private PDF in iframe

I would like to display PDF's from a non-public directory in an iframe. I have found a lot of descriptions about this problem using headers, but always ended up with the PDF filling the full page, meanwhile I would need it to be embedded into the page, e.g. besides a table inside a div.
E.g. this solution displays the PDF, but only the PDF, instead of embedding it into the application.
$file='../private/the.pdf';
$filename = 'Custom.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
#readfile($file);
echo '<iframe src="Custom.pdf" width="500px"></iframe>'`
Is it, in any way, possible to access a private PDF from an iframe?
Thank you and sorry if this was already asked before, I read quite a few issues but none of them treated embedding.
Thx,

Content-Disposition downloading wrong file

I am trying to download a file. I am using IE11. I have tried several methods to do this. Currently I am trying to use the header with Content-Disposition method. I have tried to do this a few different ways according to other answers people have given. And it does download. But instead of downloading the file I point it to, it downloads the file it is written in. So if I tell it to download example.txt in my test.php file. It will only download test.php.
These are the methods I have tried:
This one is written in test.html:
<?php
$filename = "example.txt"
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
?>
I've also tried making it a button:
BUTTON
Where download.php is:
<?php
$file = $_GET['file'];
header('Content-type: audio/mpeg');
header('Content-Disposition: attachment; filename="'.$file.'"');
?>
I tried this:
<?php
header('Content-Type: application/download');
header('Content-Disposition: attachment; filename="example.txt"');
header("Content-Length: " . filesize("example.txt"));
$fp = fopen("example.txt", "r");
fpassthru($fp);
fclose($fp);
?>
And this:
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Type: application/force-download");
header("Content-Length: " . filesize($File));
header("Connection: close");
There are many more slight variations and mix and matching that I have tried. All have the same problem that the .html or .php file downloads rather than example.txt. Does anyone know why this would happen? Is something not supported in IE11? I do not think it is a syntax error simply because most of these I copied from other answers online. I have tried with example.txt existing and not existing, in this folder and other folders.
EDIT: So it turns out that these all work, I was just using them wrong. I had been trying to make isolated files to run this code so I could test it without interference from the rest of the functions on my website, but this left the php files without the resources they needed to actually run properly. When I put the code into the actual files on the website it worked perfectly. smh
Try this :
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($File));
header('Content-Disposition: attachment; filename=' . basename($File));
readfile($File);
exit;

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

Using readfile on multiple devices?

I'm having a bad time with the readfile function on php. I'm setting up a web file server, but many of the users have problems with the downloads.
Basically, I'm using Chrome on my computer and everything works fine. Some of the users have problems on Android browser: they get a .bin file instead og .doc, or get a correct .doc file, but they can't open it. Also, someone gets a corrupted .doc file on the computer.
I have read many questions on this site but they weren't so helpful.
Since I have tried many solutions, I will post a shortened code snippet to give an idea of what I am trying to accomplish. I am using apache2, couldn't it be a configuration problem?
Does someone have my problems too?
<?php
//here I take the user, the pwd and the file RELATIVE path
//there are no errors here
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: Binary');
header('Content-disposition: attachment; filename="' . $name . '"');
readfile($file);
?>
EDIT: full code
<?php
session_start();
$data = $_SESSION['data'];
$name = $_GET['name'];
if (isset($data)) {
//private file
$file = "path/".$data."/".$name;
} else {
//public file
$file = "path/public/".$name;
}
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . $name . "\"");
readfile($file);
?>

PHP not displaying PDF file in Google Chrome

I wanted to display PDF files in browser using this code
<?php
$file = './path/to/the.pdf';
$filename = 'Custom file name for the.pdf'; /* Note: Always use .pdf at the end. */
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
#readfile($file);
?>
The code works well in Firefox, but not in Chrome. I haven't tested in any other browser. Is it a bug with Chrome? Is there anyway I could make it so it is displayed in Firefox and Chrome?
Just change the content disposition to attachment
header('Content-Disposition: attachment;; filename="' . $filename . '"');
This will not display the PDF in the browser, but in Chrome it seems to work.
It depends on your browser setting and not on the code. Usually by default firefox will open the pdf in browser whereas it is not in the case of chrome
I've had the same problem before, but not all computers had the issue. In my case I had some setting using Adobe or the default Chrome PDF reader. Check your settings.
Is $file just the path to the file without the file name itself? Is that case you should use instead:
header('Content-Length: ' . filesize("$file/$filename"));

Categories