Home  >  Article  >  Backend Development  >  61 file system

61 file system

WBOY
WBOYOriginal
2016-07-29 09:11:45881browse

Read the filecontent:

<code>PHP具有丰富的文件操作函数,最简单的<strong>读取文件</strong>的函数为file_get_contents,可以将整个文件全部读
取到一个字符串中。

<span>$content</span> = file_get_contents(<span>'./test.txt'</span>);
file_get_contents也可以通过参数控制读取内容的开始点以及长度。

<span>$content</span> = file_get_contents(<span>'./test.txt'</span>, null, null, <span>100</span>, <span>500</span>);
PHP也提供类似于C语言操作文件的方法,使用<span>fopen</span>,fgets,<span>fread</span>等方法,fgets可以从文件指针中读
取一行,freads可以读取指定长度的字符串。

<span>$fp</span> = <span>fopen</span>(<span>'./text.txt'</span>, <span>'rb'</span>);
<span>while</span>(!<span>feof</span>(<span>$fp</span>)) {
    echo fgets(<span>$fp</span>); <span>//读取一行</span>
}
<span>fclose</span>(<span>$fp</span>);


<span>$fp</span> = <span>fopen</span>(<span>'./text.txt'</span>, <span>'rb'</span>);
<span>$contents</span> = <span>''</span>;
<span>while</span>(!<span>feof</span>(<span>$fp</span>)) {
    <span>$contents</span> .= <span>fread</span>(<span>$fp</span>, <span>4096</span>); <span>//一次读取4096个字符</span>
}
<span>fclose</span>(<span>$fp</span>);
使用<span>fopen</span>打开的文件,最好使用<span>fclose</span>关闭文件指针,以避免文件句柄被占用。</code>

Determine whether the file exists:

<code>一般情况下在对文件进行操作的时候需要先判断文件是否存在,PHP中常用来判断文件存在的函数有
两个is_file与file_exists.

<span>$filename</span> = <span>'./test.txt'</span>;
<span>if</span> (file_exists(<span>$filename</span>)) {
    <span>echo</span> file_get_contents(<span>$filename</span>);
}
如果只是判断文件存在,使用file_exists就行,file_exists不仅可以判断文件是否存在,同时也可以判
断目录是否存在,从函数名可以看出,
is_file是确切的判断给定的路径是否是一个文件。

<span>$filename</span> = <span>'./test.txt'</span>;
<span>if</span> (is_file(<span>$filename</span>)) {
    <span>echo</span> file_get_contents(<span>$filename</span>);
}
更加精确的可以使用is_readable与is_writeable在文件是否存在的基础上,判断文件是否可读与可写。

<span>$filename</span> = <span>'./test.txt'</span>;
<span>if</span> (is_writeable(<span>$filename</span>)) {
    file_put_contents(<span>$filename</span>, <span>'test'</span>);
}
<span>if</span> (is_readable(<span>$filename</span>)) {
    <span>echo</span> file_get_contents(<span>$filename</span>);
}</code>

Write content to the file:

<code>与<strong>读取文件</strong>对应,PHP写文件也具有两种方式,最简单的方式是采用file_put_contents。

<span>$filename</span> = <span>'./test.txt'</span>;
<span>$data</span> = <span>'test'</span>;
file_put_contents(<span>$filename</span>, <span>$data</span>);
上例中,<span>$data</span>参数可以是一个一维数组,当<span>$data</span>是数组的时候,会自动的将数组连接起来,
相当于<span>$data</span>=implode(<span>''</span>, <span>$data</span>);

同样的,PHP也支持类似C语言风格的操作方式,采用<span>fwrite</span>进行文件写入。

<span>$fp</span> = <span>fopen</span>(<span>'./test.txt'</span>, <span>'w'</span>);
<span>fwrite</span>(<span>$fp</span>, <span>'hello'</span>);
<span>fwrite</span>(<span>$fp</span>, <span>'world'</span>);
<span>fclose</span>(<span>$fp</span>);</code>

Get the modification time of the file:

<code>文件有很多元属性,包括:文件的所有者、创建时间、修改时间、最后的访问时间等。

fileowner:获得文件的所有者
filectime:获取文件的创建时间
filemtime:获取文件的修改时间
fileatime:获取文件的访问时间
其中最常用的是文件的修改时间,通过文件的修改时间,可以判断文件的时效性,经常用在静态
文件或者缓存数据的更新。

<span>$mtime</span> = filemtime(<span>$filename</span>);
<span>echo</span><span>'修改时间:'</span>.date(<span>'Y-m-d H:i:s'</span>, filemtime(<span>$filename</span>));</code>

Get the file size:

<code>通过filesize函数可以取得文件的大小,文件大小是以字节数表示的。

<span>$filename</span> = <span>'/data/webroot/usercode/code/resource/test.txt'</span>;
<span>$size</span> = filesize(<span>$filename</span>);
如果要转换文件大小的单位,可以自己定义函数来实现。

<span><span>function</span><span>getsize</span><span>(<span>$size</span>, <span>$format</span> = <span>'kb'</span>)</span> {</span><span>$p</span> = <span>0</span>;
    <span>if</span> (<span>$format</span> == <span>'kb'</span>) {
        <span>$p</span> = <span>1</span>;
    } <span>elseif</span> (<span>$format</span> == <span>'mb'</span>) {
        <span>$p</span> = <span>2</span>;
    } <span>elseif</span> (<span>$format</span> == <span>'gb'</span>) {
        <span>$p</span> = <span>3</span>;
    }
    <span>$size</span> /= pow(<span>1024</span>, <span>$p</span>);
    <span>return</span> number_format(<span>$size</span>, <span>3</span>);
}

<span>$filename</span> = <span>'/data/webroot/usercode/code/resource/test.txt'</span>;
<span>$size</span> = filesize(<span>$filename</span>);

<span>$size</span> = getsize(<span>$size</span>, <span>'kb'</span>); <span>//进行单位转换</span><span>echo</span><span>$size</span>.<span>'kb'</span>;
值得注意的是,没法通过简单的函数来取得目录的大小,目录的大小是该目录下所有子目录以及文件
大小的总和,因此需要通过递归的方法来循环计算目录的大小。</code>

Delete files:

<code>跟Unix系统命令类似,PHP使用<span>unlink</span>函数进行文件删除。

<span>unlink</span>(<span>$filename</span>);
删除文件夹使用<span>rmdir</span>函数,文件夹必须为空,如果不为空或者没有权限则会提示失败。

<span>rmdir</span>(<span>$dir</span>);
如果文件夹中存在文件,可以先循环删除目录中的所有文件,然后再删除该目录,循环删除可以
使用<span>glob</span>函数遍历所有文件。

<span>foreach</span> (<span>glob</span>(<span>"<span>*"</span>) as <span>$filename</span>) {
   unlink(<span>$filename</span>);
}</span></code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above has introduced the 61 file system, including the content of reading files. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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