Rumah >php教程 >php手册 >简单介绍PHP获取文件属性方法

简单介绍PHP获取文件属性方法

WBOY
WBOYasal
2016-06-13 11:11:22809semak imbas

PHP获取文件属性之获取最近修改时间:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php  </span></span></span></li>
<li>
<span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">'test.txt'</span><span>;  </span>
</li>
<li class="alt"><span>echo date('r', <br>filemtime($file));  </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>

返回的说unix的时间戳,这在缓存技术常用.

相关PHP获取文件属性的还有获取上次被访问的时间fileatime(),filectime()当文件的权限,所有者,所有组或其它 inode 中的元数据被更新时间,fileowner()函数返回文件所有者

$owner = posix_getpwuid(fileowner($file));

(非window系统),ileperms()获取文件的权限,

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php  </span></span></span></li>
<li>
<span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">'dirlist.php'</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">perms</span><span> = </span><span class="attribute-value">substr</span><span>(sprintf<br>('%o', fileperms($file))<br>, -4);  </span>
</li>
<li><span>echo $perms;  </span></li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
<li><span> </span></li>
</ol>

filesize()返回文件大小的字节数:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php  </span></span></span></li>
<li><span>// 输出类似:somefile.txt:<br> 1024 bytes  </span></li>
<li class="alt">
<span>$</span><span class="attribute">filename</span><span> = </span><span class="attribute-value">'somefile.txt'</span><span>;  </span>
</li>
<li><span>echo $filename . ': ' <br>. filesize($filename) . ' bytes';  </span></li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
<li><span> </span></li>
</ol>

PHP获取文件属性的全部信息有个返回数组的函数stat()函数:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php  </span></span></span></li>
<li>
<span>$</span><span class="attribute">file</span><span> = </span><span class="attribute-value">'dirlist.php'</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">perms</span><span> = </span><span class="attribute-value">stat</span><span>($file);  </span>
</li>
<li><span>var_dump($perms);  </span></li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
<li><span> </span></li>
</ol>


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn