我如何让用户从服务器下载文件

时间:2017-08-25 05:11:04

标签: php sql-server database codeigniter

我正在使用mssql server和CI,它将db文件自动备份到某个文件夹中,现在我需要让用户将文件从服务器下载到本地计算机。

$filename = basename($_GET['file']);
  // Specify file path.
  $path = 'backups/hello.txt';
  $download_file =  $path.$filename;

  if(!empty($filename)){
      // Check file is exists on given path.
      if(file_exists($download_file))
      {
        header('Content-Disposition: attachment; filename=' . $filename);  
        readfile($download_file); 
        exit;
      }
      else
      {
        echo 'File does not exists on given path';
      }
   }
}

我试过这个,但它说文件不详。

1 个答案:

答案 0 :(得分:2)

根据php文档,你能尝试这样吗

<强>的download.php

if (file_exists($file)) {
     header('Content-Description: File Transfer');
     header('Content-Type: application/octet-stream');//change your extension of your files
     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;
  }
相关问题