1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
/** -------------------------------------------------------------------------------
 *   ファイルシステム:ファイルのダウンロードreadfileの利用例
 *  2015.09.17 作成 yoshi of CXMedia Inc.
 * -------------------------------------------------------------------------------- */
header("Content-type:text/plain; charset=UTF-8");

// ダウンロードファイルのパス
$filepath 'data/PHP_file_function.txt';

// ダウンロードの処理
if( file_exists($filepath) ){
    
header('Content-Description: File Transfer');
    
header('Content-Type: application/octet-stream');
    
header('Content-Disposition: attachment; filename='.basename($filepath));
    
header('Content-Transfer-Encoding: binary');
    
header('Expires: 0');
    
header('Cache-Control: must-revalidate');
    
header('Pragma: public');
    
header('Content-Length: '.filesize($filepath));
    
ob_clean();     //出力バッファをクリア(消去)する
    
flush();        //バックエンドの書き込みバッファをフラッシュ
    /* ------------------------------------------------------
     * readfile ( filename [,use_include_path [,context]] )
     *   use_include_path:include_pathのファイルの検索
     * ------------------------------------------------------ */
    
readfile($filepath);
    exit;
} else {
    
// ファイルがない場合
    
echo "■ファイル・ノットファンドエラー:$filepath\n";
    exit;
}

?>