3

我是初学者的php程序员。我已经编写代码来下载任何类型的文件。

当我单击下载链接时,它会转到 download.php 文件。我在本地服务器上工作,但不在服务器上工作。

我的代码是:

header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');    //application/force-download
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        //header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit();

我的代码是错误的还是服务器需要一些设置?

4

4 回答 4

1

这是在线测试的代码,它工作正常。你可以试试这个

$folder_name = $_GET['fol_name'];
$file_directory = "../img/files/$folder_name"; //Name of the directory where all the sub directories and files exists

$file = $_GET['file_name']; //Get the file from URL variable

$file_array = explode('/', $file); //Try to seperate the folders and filename from the path
$file_array_count = count($file_array); //Count the result
$filename = $file_array[$file_array_count-1]; //Trace the filename
$file_path = dirname(__FILE__).'/'.$file_directory.'/'.$file; //Set the file path w.r.t the download.php... It may be different for u

if(file_exists($file_path)) {
    header("Content-disposition: attachment; filename={$filename}"); //Tell the filename to the browser
    header('Content-type: application/octet-stream'); //Stream as a binary file! So it would force browser to download
    readfile($file_path); //Read and stream the file
}
else {
    echo "Sorry, the file does not exist!";
}

感谢你 !!

于 2014-07-05T11:34:48.387 回答
0

通过使用我编写的代码。这个问题就解决了。如果有人有同样的问题。请尝试此代码。它对我很有用。

$file_name ='../img/files'.DS.$_GET['file'];
if(is_file($file_name)) {
        if(ini_get('zlib.output_compression')) { 
                ini_set('zlib.output_compression', 'ON');       
        }
        switch(strtolower(substr(strrchr($file_name, '.'), 1))) {
                case 'pdf':  $mime = 'application/pdf'; break; // pdf files
                case 'zip':  $mime = 'application/zip'; break; // zip files
                case 'jpeg': $mime = 'image/jpeg'; break;// images jpeg
                case 'jpg':  $mime = 'image/jpg'; break;
                case 'mp3':  $mime = 'audio/mpeg'; break; // audio mp3 formats
                case 'doc':  $mime = 'application/msword'; break; // ms word
                case 'avi':  $mime = 'video/x-msvideo'; break;  // video avi format
                case 'txt':  $mime = 'text/plain'; break; // text files
                case 'xls':  $mime = 'application/vnd.ms-excel'; break; // ms excel
                default: $mime = 'application/force-download';
        }
    header('Content-Type:application/force-download');
        header('Pragma: public');       // required
        header('Expires: 0');           // no cache
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
        header('Cache-Control: private',false);
        header('Content-Type: '.$mime);
        header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
        header('Content-Transfer-Encoding: binary');
        //header('Content-Length: '.filesize($file_name));      // provide file size
        header('Connection: close');
        readfile($file_name);           
        exit();
}

感谢你!!!

于 2014-07-04T05:20:48.170 回答
0

我最近遇到了这个问题,发现这是由 ob_clean(); 引起的。和冲洗();这些导致程序下载垃圾。

我尝试了各种组合来刷新缓冲区,唯一在托管服务器上工作的组合是 ob_end_clean(); 打印$对象->正文;

它也可能适用于回声,但我没有尝试过

于 2020-04-14T13:16:08.990 回答
-1

用这个

public function loadfile($fl)
    { 
        $mime = 'application/force-download';
        header('Pragma: public');   // required
        header('Expires: 0');       // no cache
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: private',false);
        header('Content-Type: '.$mime);
        header('Content-Disposition: attachment; filename="'.basename($fl).'"');
        header('Content-Transfer-Encoding: binary');
        header('Connection: close');
        readfile($fl);      // push it out
        exit();
}
于 2013-06-25T05:54:26.693 回答