ファイルシステム 関数
PHP Manual

readfile

(PHP 4, PHP 5)

readfileファイルを出力する

説明

int readfile ( string $filename [, bool $use_include_path = false [, resource $context ]] )

ファイルを読んで標準出力に書き出します。

パラメータ

filename

読み込もうとするファイルの名前。

use_include_path

オプションの2番目の引数を使用して、これにTRUEを設定することにより、 include_path のファイルの検索も行うことができます。

context

コンテキストストリームリソース。

返り値

ファイルから読み込んだバイト数を返します。エラーが起こると FALSEを返し、また@readfile()という名前でコールされない限り、 エラーメッセージが表示されます。

例1 readfile() によるダウンロードの強制

<?php
$file 
'monkey.gif';

if (
file_exists($file)) {
    
header('Content-Description: File Transfer');
    
header('Content-Type: application/octet-stream');
    
header('Content-Disposition: attachment; filename='.basename($file));
    
header('Content-Transfer-Encoding: binary');
    
header('Expires: 0');
    
header('Cache-Control: must-revalidate');
    
header('Pragma: public');
    
header('Content-Length: ' filesize($file));
    
ob_clean();
    
flush();
    
readfile($file);
    exit;
}
?>

上の例の出力は、 たとえば以下のようになります。

開く / 保存 ダイアログ

注意

ヒント

fopen wrappers が有効の場合、この関数のファイル名として URL を使用することができます。ファイル名の指定方法に関する詳細は fopen() を参照ください。 サポートするプロトコル/ラッパー には、さまざまなラッパーの機能やその使用法、 提供される定義済み変数などの情報がまとめられています。

注意: コンテキストのサポートは、 PHP 5.0.0 で追加されました。contexts の説明に関しては、 ストリーム を参照してください。

参考


ファイルシステム 関数
PHP Manual