Need to display pdf's uploaded to secure folder

In the document management system I am building, I needed to secure the pdf’s away from the browser so have them being uploaded to a folder called docs in the cgi-bin folder, works really well as nobody can access them throguh the browser.

But can someon give me some advice as to the process of allowing those pdf’s to be be display, I have read that you echo them out, but wondered if someone could give me the correct explanation for it so I can google it and try and work it out.

http://stackoverflow.com/questions/4679756/show-a-pdf-files-in-users-browser-via-php-perl

It really is a matter of just printing the contents of the file after properly setting your headers.

Right ok,

I have made a start and it seems to make sense, and I am getting the grey pdf loading colour, but the file isnt coming through.


<a href="spdf.php?pdf=<?php echo $id?>" target="_blank">View</a></td>

That calls spdf.php and take across with it the ID of the document.

On spdf.php I have this


<?php
require_once('auth.php');
include "config.php";
error_reporting(E_ALL);
ini_set('display_errors','On');
if(isset($_GET['pdf']))
{
 $stock_Id=$_GET['pdf'];
 $q=mysql_query("select Document from Documents where ID=$stock_Id") or die (mysql_error()); 
 $cdocdocb=$rows["Document"];
}
$file='$cdocdocb';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="$file"');
@readfile($file);
?>


$file - is the exact path and the name of the pdf as below in this case -


./cgi-bin/docs/217465424-Fifty-Years-of-the-Deep-State.pdf


<?php
require_once('auth.php');
include "config.php";
error_reporting(E_ALL);
ini_set('display_errors','On');
if(isset($_GET['pdf']))
{
 $stock_Id=$_GET['pdf'];
 $q=mysql_query("select Document from Documents where ID=$stock_Id") or die (mysql_error());
 $cdocdocb=$rows["Document"];
}
$file='$cdocdocb';
//header('Content-type: application/pdf');
//header('Content-Disposition: inline; filename="$file"');
//@readfile($file);
echo "<p>$file</p>";
?>

What does that print out in the browser.

Notice: Undefined variable: rows in /home/linweb30/u/utgroup.co.uk/user/htdocs/admin/spdf.php on line 14
$cdocdocb

So changed below to -


$cdocdocb=$q["Document"];

And this time the output was simple -

$cdocdocb

You have a logic error. Single quotes don’t expand. Though I’m having a hard time understanding why that line is there in the first place but we will go with it.


<?php 
require_once('auth.php'); 
include "config.php"; 
error_reporting(E_ALL); 
ini_set('display_errors','On'); 
if(isset($_GET['pdf'])) 
{ 
 $stock_Id=$_GET['pdf']; 
 $q=mysql_query("select Document from Documents where ID=$stock_Id") or die (mysql_error());  
 $cdocdocb=$rows["Document"]; // cdocdocb - what does that even mean&#8230;
} 
$file="$cdocdocb"; // Dont' understand why this is even here&#8230;
//header('Content-type: application/pdf'); 
//header('Content-Disposition: inline; filename="$file"'); 
//@readfile($file); 
echo "<p>$file</p>"; 
?>

try that.

I changed it slightly to this on the error that came back, and it came back with the path to the pdf


<?php 
require_once('auth.php'); 
include "config.php"; 
error_reporting(E_ALL); 
ini_set('display_errors','On'); 
if(isset($_GET['pdf'])) 
{ 
 $stock_Id=$_GET['pdf']; 
 $a=mysql_query("select Document from Documents where ID=$stock_Id") or die (mysql_error());
 while($s=mysql_fetch_assoc($a)){ 
 $cdocdocb=$s["Document"]; // cdocdocb - what does that even mean&#8230;
} 
}
$file="$cdocdocb"; // Dont' understand why this is even here&#8230;
//header('Content-type: application/pdf'); 
//header('Content-Disposition: inline; filename="$file"'); 
//@readfile($file); 
echo "<p>$file</p>"; 
?> 



../cgi-bin/docs/217465424-Fifty-Years-of-the-Deep-State.pdf


Thanks for the help oddz, works perfectly…

Cheers

Hi again, am making sure I reply in this thread as is still on topic.

The pdf and everything above works fine, no worries there, but wondered if there anyway of having a small bit of html appear at the top of the page, so pushing the pdf down a bit to accomodate it.

Basically they wont to put a close button across the top of the page, and everything i try the html is ignored.

Something liek this I have been trying, but its not the way obviously, if it can be done at all i suppose.


<?php 
session_start();
require_once('../auth.php'); 
include "../config.php"; 
error_reporting(E_ALL); 
ini_set('display_errors','On'); 
if(isset($_GET['pdf'])) 
{ 
 $stock_Id=$_GET['pdf']; 
 $a=mysql_query("select Document from Documents where ID=$stock_Id") or die (mysql_error());
 while($s=mysql_fetch_assoc($a)){ 
 $cdocdocb=$s["Document"]; // cdocdocb - what does that even mean&#8230;
} 
}
$file="$cdocdocb"; // Dont' understand why this is even here&#8230;
header('Content-type: application/pdf'); 
header('Content-Disposition: inline; filename="$file"'); 
//@readfile($file); 
echo "<div style='position:relative; width:100%; height:30px;'>123</div>";
echo "<p>"[EMAIL=".@readfile($file)."</p"].@readfile($file)."</p[/EMAIL]>"; 
?> 

So now you need two scripts (or one script and some plain HTML).

The PDF script will be unchanged - the only difference is that you will need to reference it from an <object> or <img> tag from within the HTML that is providing the rest of the page content.

Hi felgall,

Right I see, could you give me an idea of what you mean.

Do you mean sort of liek this


<?php 
session_start();
require_once('../auth.php'); 
include "../config.php"; 
error_reporting(E_ALL); 
ini_set('display_errors','On'); 
if(isset($_GET['pdf'])) 
{ 
 $stock_Id=$_GET['pdf']; 
 $a=mysql_query("select Document from Documents where ID=$stock_Id") or die (mysql_error());
 while($s=mysql_fetch_assoc($a)){ 
 $cdocdocb=$s["Document"]; // cdocdocb - what does that even mean&#8230;
} 
}
$file="$cdocdocb"; // Dont' understand why this is even here&#8230;
header('Content-type: application/pdf'); 
header('Content-Disposition: inline; filename="$file"'); 
//@readfile($file);  
?>
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>utgroup admin</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-type" />
</head>
<body>
<div style="position:relative; width:100%; height:30px; background-color:#FF0000; margin-bottom:40px; clear:both;"></div>
<img src="<?php echo @readfile($file)?>" />
</body>
</html>


What I got isnt working though

You need TWO files.

File one generates the PDF

File two contains the HTML and attaches the PDF into the page

These two files are producing completely different file types - one is creating a PDF and the other is creating HTML.

The PDF can be attached into the page using an <object> or <img> tag.

Hi felgall,

I’m sorry Im coming back to you but I just cant work out what you mean and cant find anything about it on the web to help me out, could you assist me a bit further with it.

Everything I try the pdf output overrides it and the pdf is displayed full page.

Im trying to basically have a div at the top of about 40px height, 100% width to allow me to put a close button in it.

Thanks

I have made some progress, but this time the pdf isnt displaying, i get files does not begin with %pdf, so its nearly there, and this is what i got.

link from page calls html page


<body>
<div style="position:relative; width:100%; height:30px; background-color:#FF0000; margin-bottom:40px; clear:both;"></div>

<object data="spdf.php" type="application/pdf" width="100%" height="100%">
 
  <p>It appears you don't have a PDF plugin for this browser.
  No biggie... you can <a href="myfile.pdf">click here to
  download the PDF file.</a></p>
  
</object>

</body>
</html>

Calling the page that creates the pdf



<?php 
session_start();
require_once('../auth.php'); 
include "../config.php"; 
error_reporting(E_ALL); 
ini_set('display_errors','On'); 
if(isset($_GET['pdf'])) 
{ 
 $stock_Id=$_GET['pdf']; 
 $a=mysql_query("select Document from Documents where ID=$stock_Id") or die (mysql_error());
 while($s=mysql_fetch_assoc($a)){ 
 $cdocdocb=$s["Document"];
} 
}
$file="$cdocdocb";
header('Content-type: application/pdf'); 
header('Content-Disposition: inline; filename="$file"'); 
@readfile($file); 
//echo "<p>$file</p>"; 
?> 

I thought something along these lines would cure it, but again the pdf when displayed takes over the full browser window, and no sign of any html.


<?php 
session_start();
require_once('../auth.php'); 
include "../config.php"; 
error_reporting(E_ALL); 
ini_set('display_errors','On'); 
if(isset($_GET['pdf'])) 
{ 
 $stock_Id=$_GET['pdf']; 
 $a=mysql_query("select Document from Documents where ID=$stock_Id") or die (mysql_error());
 while($s=mysql_fetch_assoc($a)){ 
 $cdocdocb=$s["Document"];
} 
}
$file="$cdocdocb";
header('Content-type: application/pdf'); 
header('Content-Disposition: inline; filename="$file"'); 
//@readfile($file); 
//echo "<p>$file</p>"; 
?>


<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>utgroup admin</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-type" />
</head>
<body>

<div style="position:relative; width:100%; height:30px; background-color:#FF0000; margin-bottom:40px; clear:both;"></div>

<object data="<?php echo @readfile($file);?>" type="application/pdf" width="100%" height="100%">
 <p>It appears you don't have a PDF plugin for this browser. No biggie... you can <a href="myfile.pdf">click here to download the PDF file.</a></p>
</object>

</body>
</html>