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

时间:2021-07-20 03:48:37

I am using mssql server and CI in which it auto backups the db file into certain folder now I need to let the user download the file from the server to her local machine.

我正在使用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';
      }
   }
}

I have tried this one but it says file is unknown.

我试过这个,但它说文件未知。

1 个解决方案

#1


0  

As per php document, can you try like this

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

download.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;
  }

#1


0  

As per php document, can you try like this

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

download.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;
  }