file_get_contents()可以將文件讀入字符串。此函數(shù)是用來(lái)將文件的內(nèi)容讀入到一個(gè)字符串中的首選方法。如果操作系統(tǒng)支持還會(huì)使用內(nèi)存映射技術(shù)來(lái)增強(qiáng)性能。
string file_get_contents ( string $filename [, bool $use_include_path = FALSE [, resource $context [, int $offset = 0 [, int $maxlen ]]]] )
此函數(shù)類(lèi)似于file()函數(shù),不同之處在于,file_get_content()函數(shù)返回字符串中的文件,該字符串從指定偏移量開(kāi)始,最大可達(dá)maxlen字節(jié)。
參數(shù) | 描述 |
---|---|
filename | 必需。指定要讀取的文件。 |
use_include_path | 可選。如果您還想在 use_include_path(在 php.ini 中)中搜索文件的話(huà),請(qǐng)?jiān)O(shè)置該參數(shù)為 '1'。 |
context | 可選。指定文件句柄的環(huán)境。context 是一套可以修改流的行為的選項(xiàng)。若使用 NULL,則忽略。 |
offset | 可選。指定在文件中開(kāi)始讀取的位置。該參數(shù)是 PHP 5.1 中新增的。 |
maxlen | 可選。指定讀取的字節(jié)數(shù)。該參數(shù)是 PHP 5.1 中新增的。 |
<?php $file = file_get_contents("/PhpProject/sample.txt", true); echo $file; ?>
輸出結(jié)果
www.jixiangtaizi.com.cn nhooo
<?php $section = file_get_contents("/PhpProject/sample.txt", NULL, NULL, 4, 10); var_dump($section); ?>
輸出結(jié)果
string(10) "(cainiaoplus.com)"