Heim  >  Artikel  >  php教程  >  php file 函数

php file 函数

WBOY
WBOYOriginal
2016-06-13 11:17:282065Durchsuche

php file 函数  

file
( PHP 4中, PHP 5中)

file-读取整个file到一个数组

描述
一系列file(字符串$file[摘要$国旗= 0 [ ,资源$背景] ] )
读取整个file到一个数组。

注:您可以使用file_get_contents ( )返回file内容作为一个字符串。


参数

file名
file路径。

提示
网址可以用来作为file名与此功能,如果打开包装已启用。见fopen ( )函数的更多细节关于如何指定file名和列表支持的协议/封装协议的列表,支持的URL协议。

旗帜
可选参数可以是一个标志,或以上,以下常数:

FILE_USE_INCLUDE_PATH
搜索该file中的include_path中。
FILE_IGNORE_NEW_LINES
不添加新行结束时,每个数组元素
FILE_SKIP_EMPTY_LINES
跳过空行
FILE_TEXT
返回的内容是在UTF - 8编码。您可以指定一个不同的编码,建立一个自定义的范围内。此标志不能用于FILE_BINARY 。此标志只适用于自PHP 6 。
FILE_BINARY
内容改为二进制数据。这是默认设置,并不能用于FILE_TEXT 。此标志只适用于自PHP 6 。

背景
背景资源创建的stream_context_create ( )函数。


注:情况下,支持加用PHP 5.0.0 。为说明情况,请参阅流功能。



返回值
返回file中的数组。每个元素的数组对应行中的file,仍与换行符重视。一旦失败,file( )返回FALSE 。

注意:每行中所产生的阵列将包括行结尾,除非FILE_IGNORE_NEW_LINES使用,所以你仍然需要使用rtrim ( )如果你不想结束本线。


注:如果PHP没有正确认识到行结尾时,无论是阅读file或创建的Macintosh电脑,使auto_detect_line_endings运行时配置选项可能有助于解决这个问题。

修改

版本说明
6.0.0新增了支援FILE_TEXT和FILE_BINARY旗帜。
5.0.0背景参数增加
5.0.0到PHP 5.0.0之前的旗帜参数只覆盖include_path中,并启用了1
4.3.0file( )成为二进制安全


实例

例如1号file( )的例子

// Get a file into an array.  In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('http://www.example.com/');

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
    echo "Line #{$line_num} : " . htmlspecialchars($line) . "
n";
}

// Another example, let's get a web page into a string.  See also file_get_contents().
$html = implode('', file('http://www.example.com/'));

// Using the optional flags parameter since PHP 5
$trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
?>


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn