1. Commonly used file function library
1. basename(); -- Returns the file name part of the path.
<span style="color: #0000ff;">string</span> <span style="color: #008080;">basename</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$path</span> [, <span style="color: #0000ff;">string</span> <span style="color: #800080;">$suffix</span><span style="color: #000000;"> ] )
//给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件名。</span> Copy after login
Parameter: path
A path. In Windows, both slashes (/ ) and backslashes ( ) can be used as directory separators. In other environments it is a slash (/ )
suffix
If the file name ends with suffix
, then this part will also be removed.
Return value: Return the basic file name of path
.
<span style="color: #800080;">$path</span> = 'd:/test/test.txt'<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> <span style="color: #008080;">basename</span>(<span style="color: #800080;">$path</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> <span style="color: #008080;">basename</span>(<span style="color: #800080;">$path</span>,'.txt'); Copy after login
2. dirname(); -- Returns the directory part of the path
<span style="color: #0000ff;">string</span> <span style="color: #008080;">dirname</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$path</span><span style="color: #000000;"> )
</span><span style="color: #008000;">//</span><span style="color: #008000;">给出一个包含有指向一个文件的全路径的字符串,本函数返回去掉文件名后的目录名。</span> Copy after login
Parameters: path,
a path. In Windows, both slashes (/ ) and backslashes ( ) can be used as directory separators. In other contexts it is a slash (/ ).
Return value : Return the parent directory of path. If there is no slash in path
, a dot ('. ') is returned, indicating the current directory. Otherwise, the string returned is the string after removing the
/component (the last slash and the following part) at the end of path .
<span style="color: #800080;">$path</span> = 'd:/test/test.txt'<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> <span style="color: #008080;">basename</span>(<span style="color: #800080;">$path</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> <span style="color: #008080;">basename</span>(<span style="color: #800080;">$path</span>,'.txt'<span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> <span style="color: #008080;">dirname</span>(<span style="color: #800080;">$path</span>); Copy after login
3. pathinfo(); --Return file path information
<span style="color: #0000ff;">mixed</span> <span style="color: #008080;">pathinfo</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$path</span> [, int <span style="color: #800080;">$options</span> = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION |<span style="color: #000000;"> PATHINFO_FILENAME ] )
</span><span style="color: #008000;">//</span><span style="color: #008000;">pathinfo() 返回一个关联数组包含有 path 的信息。返回关联数组还是字符串取决于 options。</span> Copy after login
<strong><span style="font-size: 16px;">参数</span></strong><span class="genanchor"><span class="Apple-converted-space"><span style="font-size: 16px;"><strong>:</strong> <code class="parameter">path </code></span><code class="parameter"> </code></span></span><span style="font-size: 16px;">要解析的路径。<br /> </span><code class="parameter"><span style="font-size: 16px;">options</span> </code><span style="font-size: 16px;">如果指定了,将会返回指定元素;它们包括:<code>PATHINFO_DIRNAME</code>,<code>PATHINFO_BASENAME</code><span class="Apple-converted-space"> 和<code>PATHINFO_EXTENSION</code><span class="Apple-converted-space"> 或<span class="Apple-converted-space"> <code>PATHINFO_FILENAME</code>。</span></span></span>如果没有指定</span><span class="Apple-converted-space"><span style="font-size: 16px;"> <code class="parameter">options</code></span><span class="Apple-converted-space"><span style="font-size: 16px;"> 默认是返回全部的单元。<br /> <strong>返回值:</strong>如果没有传入<span class="Apple-converted-space"> <code class="parameter">options</code><span class="Apple-converted-space"> ,将会返回包括以下单元的数组<span class="Apple-converted-space"> <span class="type">array:<em>dirname</em>,<em>basename</em><span class="Apple-converted-space"> 和<span class="Apple-converted-space"> <em>extension</em>(如果有),以 及<em>filename</em>。</span></span></span></span></span></span> <br /></span></span></span> Copy after login
<span style="color: #800080;">$path</span> = 'd:/test/test.txt'<span style="color: #000000;">;
</span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">pathinfo</span>(<span style="color: #800080;">$path</span>)); Copy after login
4. filetype();--Get the file type
<span style="color: #0000ff;">string</span> <span style="color: #008080;">filetype</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$filename</span><span style="color: #000000;"> )
</span><span style="color: #008000;">//</span><span style="color: #008000;">返回文件的类型。</span> Copy after login
<strong><span style="font-size: 16px;">参数</span></strong>: <span style="font-size: 16px;"><code class="parameter">filename </code>文件</span><span style="font-size: 16px;">的路径。</span> Copy after login
<span style="font-size: 16px;"><strong> 返回值</strong></span>: <span style="font-size: 16px;">返回文件的类型。 可能的值有 fifo,char,dir,block,link,file 和 unknown。如果出错则返回</span><span class="Apple-converted-space"><span style="font-size: 16px;"> <code>FALSE</code>。如果 stat 调用失败或者文件类型未知的话</span><span class="Apple-converted-space"><span style="font-size: 16px;"> </span><span class="function"><span style="font-size: 16px;">filetype()</span><span class="Apple-converted-space"><span style="font-size: 16px;"> 还会产生一个</span><span class="Apple-converted-space"><span style="font-size: 16px;"> <code>E_NOTICE</code></span><span class="Apple-converted-space"><span style="font-size: 16px;"> 消息。</span></span></span></span></span></span></span> Copy after login
<span style="color: #800080;">$path</span> = 'd:/test/test.txt'<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> <span style="color: #008080;">filetype</span>(<span style="color: #800080;">$path</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">结果file</span> Copy after login
5. fstat() and stat();
⑴, fstat()-obtain file information through the opened file pointer
<span style="color: #0000ff;">array</span> <span style="color: #008080;">fstat</span> ( <span style="color: #0000ff;">resource</span> <span style="color: #800080;">$handle</span><span style="color: #000000;"> )
</span><span style="color: #008000;">//</span><span style="color: #008000;">获取由文件指针 handle 所打开文件的统计信息。本函数和 stat() 函数相似,除了它是作用于已打开的文件指针而不是文件名。</span> Copy after login
Parameters: <span style="font-size: 16px;"> handle</span>
The file system pointer is typically created by fopen() resource (resource).
Return value : Returns an array with statistical information for the file. The format of the array is detailed in the manualstat() page.
⑵、stat() --gives file information
<span style="color: #0000ff;">array</span> <span style="color: #008080;">stat</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$filename</span><span style="color: #000000;"> )
</span><span style="color: #008000;">//</span><span style="color: #008000;">获取由 filename 指定的文件的统计信息。如果 filename 是符号连接,则统计信息是关于被连接文件本身的,而不是符号连接。
//lstat() 和 stat() 相同,只除了它会返回符号连接的状态。</span> Copy after login
Parameter:filename
The path of the file.
<span style="color: #800080;">$path</span> = 'd:/test/test.txt'<span style="color: #000000;">;
</span><span style="color: #800080;">$fp</span> = <span style="color: #008080;">fopen</span>("d:/test/test.txt","r"<span style="color: #000000;">);
</span><span style="color: #800080;">$fstat</span> = <span style="color: #008080;">fstat</span>(<span style="color: #800080;">$fp</span><span style="color: #000000;">);
</span><span style="color: #008080;">fclose</span>(<span style="color: #800080;">$fp</span><span style="color: #000000;">);
</span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$fstat</span>); Copy after login
6、filesize();--取得文件大小
int <span style="color: #008080;">filesize</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$filename</span><span style="color: #000000;"> )
</span><span style="color: #008000;">//</span><span style="color: #008000;">取得指定文件的大小。</span> Copy after login
参数:filename
文件的路径。
返回值:返回文件大小的字节数,如果出错返回 FALSE
并生成一条 E_WARNING
级的错误。
<?<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 输出类似:test.txt: bytes</span>
<span style="color: #800080;">$filename</span> = 'd:/test/test.txt'<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$filename</span> . ': ' . <span style="color: #008080;">filesize</span>(<span style="color: #800080;">$filename</span>) . ' bytes'<span style="color: #000000;">;
</span>?>
<span style="color: #008000;">//</span><span style="color: #008000;">结果:d:/test/test.txt: 12 bytes</span> Copy after login
7、disk_free_space(); -- 返回目录中的可用空间
<span style="color: #0000ff;">float</span> <span style="color: #008080;">disk_free_space</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$directory</span><span style="color: #000000;"> )
</span><span style="color: #008000;">//</span><span style="color: #008000;">给出一个包含有一个目录的字符串,本函数将根据相应的文件系统或磁盘分区返回可用的字节数。</span> Copy after login
<span style="font-size: 16px;">参数:directory</span>
文件系统目录或者磁盘分区。
<span style="color: #008080;">header</span>("Content-Type:Text/html;charset=utf8"<span style="color: #000000;">);
</span><span style="color: #800080;">$path</span> = 'd:/test/test.txt'<span style="color: #000000;">;
</span><span style="color: #800080;">$df</span> = <span style="color: #008080;">disk_free_space</span>("d:/"<span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$df</span>."字节"; Copy after login
8、disk_total_space(); --返回一个目录的磁盘总大小
<span style="color: #0000ff;">float</span> <span style="color: #008080;">disk_total_space</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$directory</span><span style="color: #000000;"> )
</span><span style="color: #008000;">//</span><span style="color: #008000;">给出一个包含有一个目录的字符串,本函数将根据相应的文件系统或磁盘分区返回所有的字节数。 【译者注】本函数返回的是该目录所在的磁盘分区的总大小,因此在给出同一个磁盘分区的不同目录作为参数所得到的结果完全相同。 在 Unix 和 Windows 200x/XP 中都支持将一个磁盘分区加载为一个子目录,这时正确使用本函数就很有意义。</span> Copy after login
参数:directory
文件系统的目录或者磁盘分区
9、fopen($filepath,$mode)
<span style="color: #0000ff;">resource</span> <span style="color: #008080;">fopen</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$filename</span> , <span style="color: #0000ff;">string</span> <span style="color: #800080;">$mode</span> [, bool <span style="color: #800080;">$use_include_path</span> = <span style="color: #0000ff;">false</span> [, <span style="color: #0000ff;">resource</span> <span style="color: #800080;">$context</span><span style="color: #000000;"> ]] )
</span><span style="color: #008000;">//</span><span style="color: #008000;">fopen() 将 filename 指定的名字资源绑定到一个流上</span> Copy after login
Parameter: filename If filename
is in the format of "scheme://...", it will be treated as a URL, and PHP will search for the protocol processor (also known as the encapsulation protocol) to process this mode. If the wrapper protocol has not been registered for the protocol, PHP will send a message to help check for potential problems in the script and continue execution as if filename
is a normal filename.
List of possible values
fopen() in mode
mode
Description
'r'
Open in read-only mode and point the file pointer to the file header.
'r+'
Open in read-write mode and point the file pointer to the file header.
'w'
Open in writing mode, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it.
'w+'
Open in read-write mode, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it.
'a'
Open in writing mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
'a+'
Open in read-write mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
'x'
Create and open for writing, pointing the file pointer to the file header. If the file already exists, the fopen() call fails and returns FALSE
, and generates an E_WARNING
level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL|O_CREAT flag to the underlying open(2) system call.
'x+'
Create and open in read-write mode, other behaviors are the same as 'x' .
<?<span style="color: #000000;">php
</span><span style="color: #800080;">$handle</span> = <span style="color: #008080;">fopen</span>("/home/rasmus/file.txt", "r"<span style="color: #000000;">);
</span><span style="color: #800080;">$handle</span> = <span style="color: #008080;">fopen</span>("/home/rasmus/file.gif", "wb"<span style="color: #000000;">);
</span><span style="color: #800080;">$handle</span> = <span style="color: #008080;">fopen</span>("http://www.example.com/", "r"<span style="color: #000000;">);
</span><span style="color: #800080;">$handle</span> = <span style="color: #008080;">fopen</span>("ftp://user:password@example.com/somefile.txt", "w"<span style="color: #000000;">);
</span>?> Copy after login
10、file();--把整个文件读入一个数组中
<span style="color: #0000ff;">array</span> <span style="color: #008080;">file</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$filename</span> [, int <span style="color: #800080;">$flags</span> = 0 [, <span style="color: #0000ff;">resource</span> <span style="color: #800080;">$context</span><span style="color: #000000;"> ]] )
</span><span style="color: #008000;">//</span><span style="color: #008000;">把整个文件读入一个数组中。</span> Copy after login
参数:filename
文件的路径。
flags
可选参数 flags
可以是以下一个或多个常量:
1
、<strong>FILE_USE_INCLUDE_PATH</strong>
在 include_path 中查找文件。 2、<strong>FILE_IGNORE_NEW_LINES</strong>
在数组每个元素的末尾不要添加换行符 3、<strong>FILE_SKIP_EMPTY_LINES</strong>
跳过空行。
<span style="font-size: 16px;">context 一个上下文资源,创建<span class="function">stream_context_create()</span>函数。</span>
<?<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 将一个文件读入数组。本例中通过 HTTP 从 URL 中取得 HTML 源文件。</span>
<span style="color: #800080;">$lines</span> = <span style="color: #008080;">file</span>('http://www.example.com/'<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 在数组中循环,显示 HTML 的源文件并加上行号。</span>
<span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$lines</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$line_num</span> => <span style="color: #800080;">$line</span><span style="color: #000000;">) {
</span><span style="color: #0000ff;">echo</span> "Line #<b>{<span style="color: #800080;">$line_num</span>}</b> : " . <span style="color: #008080;">htmlspecialchars</span>(<span style="color: #800080;">$line</span>) . "<br />\n"<span style="color: #000000;">;
}
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 另一个例子将 web 页面读入字符串。参见 file_get_contents()。</span>
<span style="color: #800080;">$html</span> = <span style="color: #008080;">implode</span>('', <span style="color: #008080;">file</span>('http://www.example.com/'<span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 从 PHP 5 开始可以使用可选标记参数</span>
<span style="color: #800080;">$trimmed</span> = <span style="color: #008080;">file</span>('somefile.txt', FILE_IGNORE_NEW_LINES |<span style="color: #000000;"> FILE_SKIP_EMPTY_LINES);
</span>?> Copy after login
11、file_get_contents();-- 将整个文件读入一个字符串
<span style="color: #0000ff;">string</span> <span style="color: #008080;">file_get_contents</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$filename</span> [, bool <span style="color: #800080;">$use_include_path</span> = <span style="color: #0000ff;">false</span> [, <span style="color: #0000ff;">resource</span> <span style="color: #800080;">$context</span> [, int <span style="color: #800080;">$offset</span> = -1 [, int <span style="color: #800080;">$maxlen</span><span style="color: #000000;"> ]]]] )
</span><span style="color: #008000;">//</span><span style="color: #008000;">和 file() 一样,只除了 file_get_contents() 把文件读入一个字符串。将在参数 offset 所指定的位置开始读取长度为 maxlen 的内容。如果失败,file_get_contents() 将返回 FALSE。</span> Copy after login
参数:filename:
要读取的文件的名称。
use_include_path:As of PHP 5 the FILE_USE_INCLUDE_PATH
can be used to trigger include path search.
context:A valid context resource created with stream_context_create(). 如果你不需要自定义 context,可以用 NULL
来忽略。
<span style="color: #008080;">header</span>("Content-Type:Text/html;charset=utf8"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> <= PHP 5</span>
<span style="color: #800080;">$file</span> = <span style="color: #008080;">file_get_contents</span>('d:/test/test.txt', <span style="color: #0000ff;">true</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$file</span>.'<br>'<span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> > PHP 5</span>
<span style="color: #800080;">$file</span> = <span style="color: #008080;">file_get_contents</span>('d:/test/test.txt',<span style="color: #000000;"> FILE_USE_INCLUDE_PATH);
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$file</span><span style="color: #000000;">;
</span><span style="color: #008000;">//</span><span style="color: #008000;">结果
//this is test
//this is test</span> Copy after login
12、fgets();--从文件指针中读取一行
<span style="color: #0000ff;">string</span> <span style="color: #008080;">fgets</span> ( <span style="color: #0000ff;">resource</span> <span style="color: #800080;">$handle</span> [, int <span style="color: #800080;">$length</span><span style="color: #000000;"> ] )
</span><span style="color: #008000;">//</span><span style="color: #008000;">从文件指针中读取一行。</span> Copy after login
参数:handle:
文件指针必须是有效的,必须指向由 fopen() 或 fsockopen() 成功打开的文件(并还未由 fclose() 关闭)。
length
:从 handle
指向的文件中读取一行并返回长度最多为 length
- 1 字节的字符串。碰到换行符(包括在返回值中)、EOF 或者已经读取了 length - 1 字节后停止(看先碰到那一种情况)。如果没有指定length
,则默认为 1K,或者说 1024 字节。
13、ftell();-- 返回文件指针读/写的位置
int <span style="color: #008080;">ftell</span> ( <span style="color: #0000ff;">resource</span> <span style="color: #800080;">$handle</span><span style="color: #000000;"> )
</span><span style="color: #008000;">//</span><span style="color: #008000;">返回由 handle 指定的文件指针的位置,也就是文件流中的偏移量。</span> Copy after login
参数:handle :
文件指针必须是有效的,且必须指向一个通过 fopen() 或 popen() 成功打开的文件。在附加模式(加参数 "a" 打开文件)中 ftell() 会返回未定义错误。
<span style="color: #008080;">header</span>("Content-Type:Text/html;charset=utf8"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> opens a file and read some data</span>
<span style="color: #800080;">$fp</span> = <span style="color: #008080;">fopen</span>("d:/test/test.txt", "r"<span style="color: #000000;">);
</span><span style="color: #800080;">$data</span> = <span style="color: #008080;">fgets</span>(<span style="color: #800080;">$fp</span>, 4<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> where are we ?</span>
<span style="color: #0000ff;">echo</span> <span style="color: #008080;">ftell</span>(<span style="color: #800080;">$fp</span>); <span style="color: #008000;">//</span><span style="color: #008000;"> 结果3</span>
<span style="color: #008080;">fclose</span>(<span style="color: #800080;">$fp</span>); Copy after login
14、fseek();--在文件指针中定位
int <span style="color: #008080;">fseek</span> ( <span style="color: #0000ff;">resource</span> <span style="color: #800080;">$handle</span> , int <span style="color: #800080;">$offset</span> [, int <span style="color: #800080;">$whence</span> =<span style="color: #000000;"> SEEK_SET ] )
</span><span style="color: #008000;">//</span><span style="color: #008000;">在与 handle 关联的文件中设定文件指针位置。 新位置从文件头开始以字节数度量,是以 whence 指定的位置加上 offset。</span> Copy after login
参数 :handle:
文件系统指针,是典型地由 fopen() 创建的 resource(资源)。
offset:
偏移量。要移动到文件尾之前的位置,需要给 offset
传递一个负值,并设置 whence
为 SEEK_END
。
whence
values are: 1、SEEK_SET
- 设定位置等于 offset
字节。2、 SEEK_CUR
- 设定位置为当前位置加上 offset
。2、 SEEK_END
- 设定位置为文件尾加上 <span style="font-size: 16px;">offset。</span>
<span style="color: #008080;">header</span>("Content-Type:Text/html;charset=utf8"<span style="color: #000000;">);
</span><span style="color: #800080;">$fp</span> = <span style="color: #008080;">fopen</span>('d:\test\test.txt', 'r'<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> read some data</span>
<span style="color: #800080;">$data</span> = <span style="color: #008080;">fgets</span>(<span style="color: #800080;">$fp</span>, 4096<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> move back to the beginning of the file
// same as rewind($fp);</span>
<span style="color: #008080;">fseek</span>(<span style="color: #800080;">$fp</span>, 0); Copy after login
15、flock();--轻便的咨询文件锁定
bool <span style="color: #008080;">flock</span> ( <span style="color: #0000ff;">resource</span> <span style="color: #800080;">$handle</span> , int <span style="color: #800080;">$operation</span> [, int &<span style="color: #800080;">$wouldblock</span><span style="color: #000000;"> ] )
</span><span style="color: #008000;">//</span><span style="color: #008000;">flock() 允许执行一个简单的可以在任何平台中使用的读取/写入模型(包括大部分的 Unix 派生版和甚至是 Windows)。</span> Copy after login
参数:handle
文件系统指针,是典型地由 fopen() 创建的 resource(资源)。
operation
可以是以下值之一:1、 LOCK_SH
取得共享锁定(读取的程序)。2、LOCK_EX
取得独占锁定(写入的程序。3、 LOCK_UN
释放锁定(无论共享或独占)。
如果不希望 flock() 在锁定时堵塞,则是 LOCK_NB
(Windows 上还不支持)。
wouldblock:
如果锁定会堵塞的话(EWOULDBLOCK 错误码情况下),可选的第三个参数会被设置为 TRUE
。(Windows 上不支持)
<span style="color: #0000ff;">if</span> (<span style="color: #008080;">flock</span>(<span style="color: #800080;">$fp</span>, LOCK_EX)) { <span style="color: #008000;">//</span><span style="color: #008000;"> 进行排它型锁定</span>
<span style="color: #008080;">ftruncate</span>(<span style="color: #800080;">$fp</span>, 0); <span style="color: #008000;">//</span><span style="color: #008000;"> truncate file</span>
<span style="color: #008080;">fwrite</span>(<span style="color: #800080;">$fp</span>, "Write something here\n"<span style="color: #000000;">);
</span><span style="color: #008080;">fflush</span>(<span style="color: #800080;">$fp</span>); <span style="color: #008000;">//</span><span style="color: #008000;"> flush output before releasing the lock</span>
<span style="color: #008080;">flock</span>(<span style="color: #800080;">$fp</span>, LOCK_UN); <span style="color: #008000;">//</span><span style="color: #008000;"> 释放锁定</span>
} <span style="color: #0000ff;">else</span><span style="color: #000000;"> {
</span><span style="color: #0000ff;">echo</span> "Couldn't get the lock!"<span style="color: #000000;">;
}
</span><span style="color: #008080;">fclose</span>(<span style="color: #800080;">$fp</span>); Copy after login
16、is_readable --判断给定文件名是否可读
bool <span style="color: #008080;">is_readable</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$filename</span><span style="color: #000000;"> )
</span><span style="color: #008000;">//</span><span style="color: #008000;">判断给定文件名是否存在并且可读。</span> Copy after login
参数:filename:
文件的路径。
返回值:如果由 filename
指定的文件或目录存在并且可读则返回 TRUE
,否则返回 FALSE
。
<span style="color: #800080;">$filename</span> = 'd:\test\test.txt'<span style="color: #000000;">;
</span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">is_readable</span>(<span style="color: #800080;">$filename</span><span style="color: #000000;">)) {
</span><span style="color: #0000ff;">echo</span> 'The file is readable'<span style="color: #000000;">;
} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
</span><span style="color: #0000ff;">echo</span> 'The file is not readable'<span style="color: #000000;">;
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">The file is readable</span> Copy after login
17、is_writeable -- 判断给定的文件名是否可写
bool <span style="color: #008080;">is_writable</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$filename</span><span style="color: #000000;"> )
</span><span style="color: #008000;">//</span><span style="color: #008000;">如果文件存在并且可写则返回 TRUE。filename 参数可以是一个允许进行是否可写检查的目录名。</span> Copy after login
参数:filename
要检查的文件名称。
<span style="color: #800080;">$filename</span> = 'd:\test\test.txt'<span style="color: #000000;">;
</span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">is_writeable</span>(<span style="color: #800080;">$filename</span><span style="color: #000000;">)) {
</span><span style="color: #0000ff;">echo</span> 'The file is writeable'<span style="color: #000000;">;
} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
</span><span style="color: #0000ff;">echo</span> 'The file is not writeable'<span style="color: #000000;">;
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">The file is writeable</span> Copy after login
18、chown(); -- 改变文件的所有者
bool <span style="color: #008080;">chown</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$filename</span> , <span style="color: #0000ff;">mixed</span> <span style="color: #800080;">$user</span><span style="color: #000000;"> )
</span><span style="color: #008000;">//</span><span style="color: #008000;">尝试将文件 filename 的所有者改成用户 user(由用户名或用户 ID 指定)。 只有超级用户可以改变文件的所有者。</span> Copy after login
参数:filename:
文件路径。
user:
用户名或数字。
二、目录函数
1、is_dir();--判断给定文件名是否是一个目录
bool <span style="color: #008080;">is_dir</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$filename</span><span style="color: #000000;"> )
</span><span style="color: #008000;">//</span><span style="color: #008000;">判断给定文件名是否是一个目录。</span> Copy after login
参数:filename:
如果文件名存在并且为目录则返回 TRUE
。如果 filename
是一个相对路径,则按照当前工作目录检查其相对路径。
<span style="color: #800080;">$filename</span> = 'd:\test\test.txt'<span style="color: #000000;">;
</span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">is_dir</span>('$filename')); <span style="color: #008000;">//</span><span style="color: #008000;">bool(false) </span>
<span style="color: #008080;">var_dump</span>(<span style="color: #008080;">is_dir</span>('d:\test')); <span style="color: #008000;">//</span><span style="color: #008000;">bool(true)</span> Copy after login
2、mkdir();--新建目录
bool <span style="color: #008080;">mkdir</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$pathname</span> [, int <span style="color: #800080;">$mode</span> = 0777 [, bool <span style="color: #800080;">$recursive</span> = <span style="color: #0000ff;">false</span> [, <span style="color: #0000ff;">resource</span> <span style="color: #800080;">$context</span> ]]] )<br />//尝试新建一个由 pathname 指定的目录。 Copy after login
参数:pathname:
目录的路径。
mode:
默认的 mode 是 0777,意味着最大可能的访问权。有关 mode 的更多信息请阅读 chmod() 页面。
<span style="color: #008080;">mkdir</span>("d:/test/test1", 0700); Copy after login
3、opendir();--打开目录句柄
<span style="color: #0000ff;">resource</span> <span style="color: #008080;">opendir</span> ( <span style="color: #0000ff;">string</span> <span style="color: #800080;">$path</span> [, <span style="color: #0000ff;">resource</span> <span style="color: #800080;">$context</span><span style="color: #000000;"> ] )
</span><span style="color: #008000;">//</span><span style="color: #008000;">打开一个目录句柄,可用于之后的 closedir(),readdir() 和 rewinddir() 调用中。</span> Copy after login
参数:path
要打开的目录路径
context
参数的说明见手册中的 Streams API 一章。
4、readdir();--从目录句柄中读取条目
<span style="color: #0000ff;">string</span> <span style="color: #008080;">readdir</span> ([ <span style="color: #0000ff;">resource</span> <span style="color: #800080;">$dir_handle</span><span style="color: #000000;"> ] )
</span><span style="color: #008000;">//</span><span style="color: #008000;">返回目录中下一个文件的文件名。文件名以在文件系统中的排序返回。</span> Copy after login
参数:dir_handle
目录句柄的 resource,之前由 opendir() 打开
<span style="color: #008080;">header</span>("Content-Type:Text/html;charset=utf8"<span style="color: #000000;">);
</span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$handle</span> = <span style="color: #008080;">opendir</span>('d:/test'<span style="color: #000000;">)) {
</span><span style="color: #0000ff;">echo</span> "Directory handle: <span style="color: #800080;">$handle</span>\n"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "Files:\n"<span style="color: #000000;">;
</span><span style="color: #008000;">/*</span><span style="color: #008000;"> 这是正确地遍历目录方法 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">while</span> (<span style="color: #0000ff;">false</span> !== (<span style="color: #800080;">$file</span> = <span style="color: #008080;">readdir</span>(<span style="color: #800080;">$handle</span><span style="color: #000000;">))) {
</span><span style="color: #0000ff;">echo</span> "<span style="color: #800080;">$file</span>\n"<span style="color: #000000;">;
}
</span><span style="color: #008000;">/*</span><span style="color: #008000;"> 这是错误地遍历目录的方法
while ($file = readdir($handle)) {
echo "$file\n";
}
</span><span style="color: #008000;">*/</span>
<span style="color: #008080;">closedir</span>(<span style="color: #800080;">$handle</span><span style="color: #000000;">);
}</span> Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
2024-10-22 09:46:29
2024-10-13 13:53:41
2024-10-12 12:15:51
2024-10-11 22:47:31
2024-10-11 19:36:51
2024-10-11 15:50:41
2024-10-11 15:07:41
2024-10-11 14:21:21
2024-10-11 12:59:11
2024-10-11 12:17:31