我如何下载上传的文件?

时间:2016-07-28 05:53:48

标签: php file

我试图在我的数据库中下载上传的文件,但我不能。请参阅以下代码。

  

$ filepath =" upload /"。$ filename;

   <table class="main_table" border="1">
       <tr class="tb_row">
           <?php
               while($row = mysql_fetch_array($select)){
           ?>
         <td class="tb_dt"><?php echo $row['position']?></td>
         <td class="tb_dt"><?php echo $row['trainings']?></td>
         <td class="tb_dt"><?php echo $row['tr_date']?></td>
        <td><a href="download.php?name=<?php echo $row['img_path'];?>"> download </a></td>
     </tr>
 <?php        }     ?>
 </table>

1 个答案:

答案 0 :(得分:0)

使用此

download.php
<?php

  $file= $_GET['name'];// make sure it should be a correct path
  if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="' . basename($file) . '"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        readfile($file);
        exit;
    }

?>