Reputation: 1
Good morning to all. I am trying to open a pdf file in a new tab I have succesfully uploaded the pdf in the folder I want and the title of the pdf is in the database of that paricular product. from the "product list" I am using a button and open a modal where there are option of which pdf refering to that product (currently we need to upload 2 but in the future maybe more) we need to open.
This is how I call the modal:
<a href="#docs<?php echo $row['prod_id'];?>" data-target="#docs<?php echo $row['prod_id'];?>" data-toggle="modal" style="color:#fff;" class="small-box-footer"><i class="glyphicon glyphicon-duplicate text-blue"></i></a>
and this is the modal:
<div id="docs<?php echo $row['prod_id'];?>" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
<div class="modal-dialog">
<div class="modal-content" style="height:auto">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title">Supporting Documents of <?php echo $row['prod_name'];?></h4>
</div>
<div class="modal-body">
<div class="form-group">
<label class="control-label col-lg-6" for="price">Technical Datasheet</label>
<a href="view_docs.php" data-target="view_docs.php" src="../dist/docs/<?php echo $row['prod_tds'];?>"target="_blank">View TDS</a>
<div class="col-lg-9">
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-6" for="price">Material Safety Datasheet</label>
<a href="view_docs.php" data-target="view_docs.php" src="../dist/docs/<?php echo $row['prod_msds'];?>"target="_blank">View MSDS</a>
<div class="col-lg-9">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
While making this I read I need to call a new php with some headers that I donot know how to work with them and I am getting an error of "Failed to load pdf document".
This is the code on view_docs.php:
<?php session_start();
if(empty($_SESSION['id'])):
header('Location:../index.php');
endif;
include('../dist/includes/dbcon.php');
$file = 'view';
$filename = '$file'; /* Note: Always use .pdf at the end. */
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="filename.pdf"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
@readfile($file);
?>
What I am not getting correct? I believe that there is something wrong in the view_docs.php but i donk know how to make it work...
Upvotes: 0
Views: 4301
Reputation: 21661
I think your missing a few things (like the file extension):
//no extension
$file = 'view';
//this is literally $file and never used again
$filename = '$file';
header('Content-type: application/pdf');
//the name of the transfered file is filename.php (not really an issue in this case)
//but is that what you wanted there
header('Content-Disposition: inline; filename="filename.pdf"');
header('Content-Transfer-Encoding: binary');
//still no extension on 'view' so your file is just named view (nothing else)
//no file path either.
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
//and you guessed it still no extension (error suppression hides the file not found error)
@readfile($file);
The $filename
is $file
literally not the value of the variable because of the use of single quotes. PHP does not do variable interpolation (value replacement) on single quoted strings. Not that it matters because it's never used again.
The @
probably shouldn't be there or you would see failed to find the file named view
with not extension. You should be seeing some stuff like this
Warning: filesize(): stat failed for view in C:\Unit\eval\1541064182.php on line 2
Warning: readfile(view): failed to open stream: No such file or directory in C:\Unit\eval\1541064182.php on line 3
Except it will have your path and not my path to my Unit testing server.
In other words your readfile call is literately this:
@readfile('view');
You can leave the download name as filename.php
but I don't think that is the intention.
$file = 'view.pdf';
$pathname = $path.$file;
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="'.$file.'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($pathname));
header('Accept-Ranges: bytes');
readfile($pathname);
Obviously you'll have to put in the value for the path, or if its in the same folder then you can ignore that.
Oh and last thing get rid of this thing ?>
because if you have a line return after that, it may corrupt your file. I forget if that (specifically just a line return) is an issue for PDF, but any content after the closing tag (or before) will become part of the file data. The end tag is optional and so why risk it.
Upvotes: 1
Reputation: 145
You should insert PDF url in link href // view_docs.pdf
<a href="view_docs.pdf" data-target="view_docs.php" src="../dist/docs/<?php echo $row['prod_tds'];?>"target="_blank">View TDS</a>
Upvotes: 0