Reputation: 790
I am trying to show a pdf file in a viewer of sort to show the contents of the pdf. however the viewer outputs blank.
the file is located (uploaded) via a folder named 'pdf'
this is my href code to pass which pdf should be shown
<a href="viewimage.php?docnamepath='<?php echo $scannedfilepath; ?>'" class="btn btn btn-success btn-sm"
name="btnview"
id="btnview"><i class="fa-solid fa-thin fa-envelope fa-xs;"></i> View PDF </a>
this is what it looks like in the url
http://localhost/recordsmanagement/viewimage.php?docnamepath='pdf/FAQs on RA11210 and the IRR.pdf'
this is how I read the pdf
<?php
if(!isset($_SESSION)) {
session_start();
}
if(!isset($_GET['docnamepath'])) {
$docnamepath=$_GET['docnamepath'];
}
if(isset($_GET['docnamepath'])){
/** separate name and dir */
$str_arr = explode ("/",$_GET['docnamepath']);
$content = $str_arr[0];
$dir = "pdf/";
// Store the file name into variable
$file = $dir . $content;
$filename = $content;
// 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);
}
?>
the output looks like this. It is blank, what could be causing this
I got the idea by reading this example
I have already tried attaching the file in an email via php the pdf seems fine when sent via email so why is my pdf viewer not showing the pdf
UPDATE:
I have looked into @KD's comment and tried to use a file with no spaces
still does not work on my end
Upvotes: 1
Views: 113
Reputation: 790
as per KJ's comment I checked this link which is about how to get filename to get full path
and I recently discovered (I am a beginner in php) about the basename function
as shown in the answers in the link
$filename = basename($path);
instead of using this:
/** separate name and dir */
$str_arr = explode ("/",$_GET['docnamepath']);
$content = $str_arr[0];
$dir = "pdf/";
// Store the file name into variable
$file = $dir . $content;
$filename = $content;
I changed the code to this:
// Store the file name into variable
$file = $_GET['docnamepath'];
$filename = basename($path);
it shortened my code and now my pdf viewer is working.
Upvotes: 1