首页 > php教程 > php手册 > 正文

PHP常用文件函数和目录函数整理

WBOY
发布: 2016-09-02 08:42:54
原创
1054 人浏览过

一、常用文件函数库

  1、basename(); -- 返回路径中的文件名部分。

<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>
登录后复制

  参数:path 一个路径。在 Windows 中,斜线(/)和反斜线(\)都可以用作目录分隔符。在其它环境下是斜线(/

   suffix 如果文件名是以 suffix 结束的,那这一部分也会被去掉。

  返回值:返回 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');
登录后复制

 


  2、dirname(); -- 返回路径中目录部分

<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>
登录后复制

  参数:path,一个路径。在 Windows 中,斜线(/)和反斜线(\)都可以用作目录分隔符。在其它环境下是斜线(/)。

  返回值:返回 path 的父目录。 如果在 path 中没有斜线,则返回一个点('.'),表示当前目录。否则返回的是把path 中结尾的 /component(最后一个斜线以及后面部分)去掉之后的字符串。

<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>);
登录后复制

 


   3、pathinfo();  --返回文件路径的信息

<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>
登录后复制
  <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>
登录后复制
<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>));
登录后复制


  4、filetype();--取得文件类型

<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>
登录后复制
  <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>
登录后复制
<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>
登录后复制
<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>
登录后复制


  5、fstat()和stat();

   ⑴、fstat()-通过已打开的文件指针取得文件信息

<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>
登录后复制

  参数:<span style="font-size: 16px;"> handle</span> 文件系统指针,是典型地由 fopen() 创建的 resource(资源)。

返回值: 返回一个数组具有该文件的统计信息,该数组的格式详细说明于手册中 stat() 页面里。

  ⑵、stat() --给出文件的信息

<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>
登录后复制

  参数:filename 文件的路径.

<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>);
登录后复制


  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>
登录后复制

  参数: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>
登录后复制


  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>
登录后复制

  <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>."字节";
登录后复制


  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>
登录后复制

  参数: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>
登录后复制

  参数:filename 如果 filename 是 "scheme://..." 的格式,则被当成一个 URL,PHP 将搜索协议处理器(也被称为封装协议)来处理此模式。如果该协议尚未注册封装协议,PHP 将发出一条消息来帮助检查脚本中潜在的问题并将 filename 当成一个普通的文件名继续执行下去。

    

fopen() mode 的可能值列表
mode说明
'r' 只读方式打开,将文件指针指向文件头。
'r+' 读写方式打开,将文件指针指向文件头。
'w' 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
'w+' 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
'a' 写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
'a+' 读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
'x' 创建并以写入方式打开,将文件指针指向文件头。如果文件已存在,则 fopen() 调用失败并返回 FALSE,并生成一条 E_WARNING 级别的错误信息。如果文件不存在则尝试创建之。这和给 底层的 open(2) 系统调用指定 O_EXCL|O_CREAT 标记是等价的。
'x+' 创建并以读写方式打开,其他的行为和 '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>?>
登录后复制


  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>
登录后复制

  参数: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>?>
登录后复制


  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>
登录后复制

  参数: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>
登录后复制

  


  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>
登录后复制

  参数: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>
登录后复制

  参数: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>);
登录后复制


  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>
登录后复制

  参数 :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);
登录后复制


  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>
登录后复制

  参数: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>);
登录后复制


  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>
登录后复制

  参数: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>
登录后复制


  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>
登录后复制

  参数: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>
登录后复制


  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>
登录后复制

  参数: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>
登录后复制

  参数: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>
登录后复制


  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 指定的目录。
登录后复制

  参数:pathname:目录的路径。

  mode:默认的 mode 是 0777,意味着最大可能的访问权。有关 mode 的更多信息请阅读 chmod() 页面。

<span style="color: #008080;">mkdir</span>("d:/test/test1", 0700);
登录后复制


  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>
登录后复制

  参数: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>
登录后复制

  参数: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>
登录后复制

 


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!