使用php下载文件,文件存储在文件夹中,文件名存储在数据库中

时间:2016-07-23 09:00:22

标签: php mysql mysqli

我正在尝试使用php从mysqli数据库下载文件,因为我已将文件存储在文件夹中,文件名存储在mysqli数据库中。

这是我的代码......

 $query= mysqli_query($dbo,"SELECT * FROM diff_questions WHERE email = ' $email ' and status= '0' ");
$rows=mysqli_fetch_array($query);
$file=$rows['file'];
$path='http://localhost/admin1/uploads';
?>

echo "<a href='download.php?file=".$file."&path=".$path." '>Download File</a> ";

现在 的download.php -

<?php
$file = $_GET["file"];
$path = $_GET["path"];
$fullfile = $path.$file;
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" . Urlencode($file));   
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");            
header("Content-Length: " . Filesize($fullfile));
flush();
$fp = fopen($fullfile, "r");
while (!feof($fp))
{
    echo fread($fp, 65536);
    flush();
} 
fclose($fp);
?>

有人可以告诉我代码有什么问题。它不起作用。 我的意思是它工作文件直到保存文件,但它不下载完整文件imean afther下载文件大小的文件是0kb

1 个答案:

答案 0 :(得分:0)

使用以下代码替换download.php,并检查它是否适合您:

if(isset($_GET["file"])){
      $file = $_GET["file"];
      $path = $_GET["path"];
      $fullfile = $path.$file;

      header("Content-Type: application/octet-stream");
      header("Content-Disposition: attachment; filename=" . Urlencode($file));   
      header("Content-Type: application/force-download");
      header("Content-Type: application/octet-stream");
      header("Content-Type: application/download");
      header("Content-Description: File Transfer");            
      $fp = fopen($fullfile,'r');
        while ($line = fgets($fp)) {
        echo($line);
      }
      fclose($fp);
    }