2017-08-25 146 views
0

我正在使用mssql服务器和配置文件将数据库文件自动备份到某个文件夹,现在我需要让用户从服务器上下载文件到本地机器。我该如何让用户从服务器上下载文件

$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'; 
     } 
    } 
} 

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

+0

我觉得应该是'$ PATH = '备份/';'。 – Spectarion

+0

@Spectarion 消息:未定义索引:文件 – cks

+0

向我们显示您正在访问的链接。 – Spectarion

回答

0

按照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; 
    } 
+0

$ file ?????? 一个PHP错误遇到 严重性:注意 消息:未定义的变量:文件 – cks

+0

$文件是文件的位置与文件“备份/ hello.txt的” – Robert

相关问题