HTTPヘッダ、Location、ファイルの出力
作成:2006-04-20、更新:2008-12-08
HTTPヘッダ、Location、ファイルの出力。
HTTPヘッダ
headerより前に、printやincludeで出力を行なうとエラーになる。
php.iniにて、output_buffering = On ならエラーにはならないが、メモリを消費するらしい。
:(コロン)の後は、半角スペースが必要。
例(Location)
header('Location: ./guest_loginerror.php');
exit;
例(テキストを出力)
$data = "sample";
header('Content-type: text/plain');
header('Content-Length: ' . strlen($data));
echo $data;
例(画像を出力)
$filename = "test.jpg";
header('Content-type: image/jpeg');
header('Content-Length: ' . filesize($filename));
$fp = fopen($filename, 'rb');
print fread($fp, filesize($filename));
fclose($fp);