dragonsun2005 2016-07-23 09:00
浏览 64
已采纳

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

I am trying to download a file from mysqli database using php for that i have stored file in a folder and file name is stored in the mysqli database.

here is my code...

 $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> ";

and now 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);
?>

Can someone please tell me what is wrong with code. Its not working . i mean it working file till saving file but its not downloading full file imean afther downloading file size of file is 0kb

  • 写回答

1条回答 默认 最新

  • dongluanan7163 2016-07-23 12:05
    关注

    Once replace your download.php with following code and check if its working for you or not :

    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);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题