Skip to content Skip to sidebar Skip to footer

Link To A Pdf In Html, The File Has No Extension, But I Know It Is Pdf How To Make It Open Appropriately

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

Solution 1:

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.

Solution 2:

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

Post a Comment for "Link To A Pdf In Html, The File Has No Extension, But I Know It Is Pdf How To Make It Open Appropriately"