如何使用其路径存储在mysql中的php下载pdf文件?

时间:2014-09-09 10:34:00

标签: php html mysql

我正在使用此代码将pdf文件写入服务器。它将文件发送到名为DATA的文件夹。我将文件名存储到mysql。

$target = "data/";  $target = $target . basename( $_FILES['file']['name']); 

if(isset($_POST['Submit']))
{
{
$path = $_FILES['file']['name'];
}  

if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded;  
}
else
{   
echo "Sorry, there was a problem uploading your file.";
} 

现在任何人都可以帮我下载这个pdf文件。??

2 个答案:

答案 0 :(得分:0)

如果您能够在mysql中上传和存储PDF的名称,那么您将需要查询数据库并构建PDF的路径并发送下载标题以强制它在浏览器中下载

下面是一些可以帮助您的代码

<强> HTML

为每一行创建动态链接,似乎Tno是主键,因此我将下载链接作为文件参数传递

<a href="download.php?file=Tno">Download</a>

<强> PHP

//Get the file id form $_GET['file'];
$id = $_GET['file'];

//Get the row from db
$sql = "SELECT file FROM tenders WHERE Tno = $id";

//Build the path 
$file = "data/" . $mysql_row['file '];

header("Content-type:application/pdf");
// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");
// The PDF source
readfile($file);

确保在此代码

之前不要回显任何内容

答案 1 :(得分:0)

   // link for download :
        <a href="download.php"> download </a>

  //  php code inside download.php
        <?php
        //download.php
        $filename="sample.pdf"; // YOUR File name retrive from database
        $file= "data/".$filename; // YOUR Root path for pdf folder storage
        $len = filesize($file); // Calculate File Size
        ob_clean();
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: public"); 
        header("Content-Description: File Transfer");
        header("Content-Type:application/pdf"); // Send type of file
        $header="Content-Disposition: attachment; filename=$filename;"; // Send File Name
        header($header );
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: ".$len); // Send File Size
        @readfile($file);
        exit;

        ?>