3

First post. I'm working on a project for a client where they have pdf files uploaded to a file structure (LAMP Stack) but the files have no extensions on them. Under the assumption that those files have to be PDF how would I get the browsers to understand that, and open them accordingly? Obviously with adding the file extensions this would suddenly work but I can't change the way their system works, it would result in too many changes and they are on a tight deadline. As for saving a temporary copy somewhere, I could do that, but I was hoping for a better solution. Is there a way to suggest to the browsers that they open a file a certain way?

Any thoughts guys/gals?

Jeff Libman
  • 37
  • 1
  • 6
  • Now, who in their right minds leaves out a file extension? Now you have to pick up the pieces and ask us to do the dirty work in order to save their "you know what". It starts with an "a" in case you're wondering. There is a way to find out if it's a PDF etc. etc. but who's to say/know what other files on the server do not have an extension? – Funk Forty Niner Sep 10 '13 at 20:46
  • You could try this: `header('Content-Type: application/pdf'); header('Content-Disposition:inline;filename=no_file_extension_name');` but I doubt it will work. `no_file_extension_name` would be the filename in question. – Funk Forty Niner Sep 10 '13 at 20:56
  • Fred I couldn't agree with you more, I'm coming on as a contractor to finish things up, this wasn't up to me. – Jeff Libman Sep 10 '13 at 20:57
  • Try Bubba's answer below see if it works for you. You can also see http://stackoverflow.com/questions/4679756/show-a-pdf-files-in-users-browser-via-php-perl and use the code below `edit : All my problems solved. Here's the final code:` and then replace `$file = './path/to/the.pdf';` with `$file = 'example';` and making a copy of an actual PDF file and rename it to just `example`. I tried that and it worked without a file extension, yet my FF 23.0.1 automatically viewed the document with a PDF viewer plug-in. – Funk Forty Niner Sep 10 '13 at 21:09
  • There is a way to make the file downloadable by modifying the headers which will open up a prompt to save window. – Funk Forty Niner Sep 10 '13 at 21:10
  • Test my answer below. (pre-tested with a PDF with no extension). Instructions are in the answer. – Funk Forty Niner Sep 10 '13 at 21:18

2 Answers2

0

You just set the application type and file name in the headers, like so:

// This points to the file in question, note that it doesn't 
// care whether it has an extension on the name or not.
$filePathOnDisk = '/path/to/your/pdffile';

// You can make this whatever you like, it doesn't have to 
// be the same as the file name on the disk! This is the name of the file your end 
// user will see when they are asked if they want to save. open, etc in the browser.
$fileName = 'file.pdf'; 

$data = file_get_contents($filePathOnDisk); 
header("Content-type: application/pdf");
header("Content-disposition: attachment;filename=$fileName");
echo $data;

See PHP: stream remote pdf to client browser and Proper MIME media type for PDF files for reference as well.

Community
  • 1
  • 1
bubba
  • 3,762
  • 17
  • 24
  • Read the title to the question *"Link to a PDF in html, the file has no extension, but I know it is pdf how to make it open appropriately"* it has to do with PDF files with no extension. – Funk Forty Niner Sep 10 '13 at 20:48
  • In order to reflect what the OP is asking, this `$fileName = 'file.pdf';` would need to be `$fileName = 'file';` without the extension in your answer, because apparently all files have no extensions to them. – Funk Forty Niner Sep 10 '13 at 20:58
  • I did, and this is the answer to the question. When the browsers requests the PDF, the OP needs to tell the browser what the content-type is, and what the file name is. He claims he knows that that content type is PDF, so he hardcodes that in the header. He can make the filename in the header anything he arbitrarily wants to make it, it seems to wise to go ahead and put a .pdf on the end for the end user. Notice that file_get_contents does not care about the source extension (which I believe is the point you were trying to make?). – bubba Sep 10 '13 at 21:00
  • Indeed Bubba, you have a point. Now it's up to the OP to try it and see if it will work for the application in question. – Funk Forty Niner Sep 10 '13 at 21:06
  • Thx guys, got it working. I should have thought of just using headers to open it, I'm so spoiled on my firefox with adobe plugin where it just does everything for me. – Jeff Libman Sep 10 '13 at 21:19
  • @JeffLibman You're welcome Jeff. You can keep mine on file below also, you're welcome to use it. Cheers – Funk Forty Niner Sep 10 '13 at 21:21
0

Tested

You can use the following which will prompt the user to save the (PDF) file on their computer.

Notice the different file names.

One is the file that will be uploaded/prompted to the user download_example.pdf, while the other is the file without an extension as set in readfile('example');

<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="download_example.pdf"');
readfile('example');
?>
Community
  • 1
  • 1
Funk Forty Niner
  • 73,764
  • 15
  • 63
  • 131