PHP Show PDF and Images In Browser But Download Other Files
Solution:
Try this. What this is doing is that it passes in a file, and if the file is a PDF, then it will give the file to the browser, and then the browser will read the file.
<?php
// Store the file name into variable
$file = 'FileName.pdf';
$filename = 'FileName.pdf';
// Header content type
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
// Read the file
@readfile($file);
?>
Below is an image of this working in action:
Reference:
Answer
Solution:
$extenionsAllowed = ['pdf','png','jpg']; // fill all here.
// converting the extension into small case
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
// if one of the files that are to be shown
if(in_array($extension,$extenionsAllowed)){
if($extension == 'pdf'){
// pdfs are shown differently in browsers than image files
}
// code to show file
}
else{
//code to download
}
you need to tweak header to show pdfs into the browser and for images, you can just open them directly. Read it here:
Source