(PHP 4, PHP 5)
readfile — 輸出一個文件
說明
int readfile ( string$filename
[, bool $use_include_path
= false
[, resource $context
]] )
讀入一個文件并寫入到輸出緩沖。
參數
filename
要讀取的文件名。
use_include_path
如果也想在
include_path
中搜索文件,可以使用可選的第二個參數并將其設為 TRUE
。
context
A context stream resource.
返回值
返回從文件中讀入的字節數。如果出錯返回
FALSE
并且除非是以
@readfile() 形式調用,否則會顯示錯誤信息。
范例
Example #1 Forcing a download using 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;
}
?>
以上例程的輸出類似于:
注釋
Note:
readfile() will not present any memory issues, even when sending large files, on its own. If you encounter an out of memory error ensure that output buffering is off with ob_get_level().
Tip如已啟用fopen 包裝器,在此函數中, URL 可作為文件名。關于如何指定文件名詳見 fopen()。各種 wapper 的不同功能請參見 支持的協議和封裝協議,注意其用法及其可提供的預定義變量。
Note: 在 PHP 5.0.0 中增加了對上下文(Context)的支持。有關上下文(Context)的說明參見 Streams。
參見
fpassthru() - 輸出文件指針處的所有剩余數據 file() - 把整個文件讀入一個數組中 fopen() - 打開文件或者 URL include - include require - require virtual() - 執行 Apache 子請求 file_get_contents() - 將整個文件讀入一個字符串 支持的協議和封裝協議