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 に答える
-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 に答える