Rumah  >  Artikel  >  php教程  >  php中检查文件或目录是否存在的代码小结

php中检查文件或目录是否存在的代码小结

WBOY
WBOYasal
2016-10-28 15:03:281122semak imbas

检查文件或目录是否存在 ,我们使用了php中常用的函数file_exists,这个函数就可以实现我想要的功能,下面大家慢慢参考一下
下面是一个简单的检查文件是否存在的实例代码:

<span style="color: #000000">php
</span><span style="color: #800080">$filename</span> = '/path/to/foo.txt'<span style="color: #000000">;
</span><span style="color: #0000ff">if</span> (<span style="color: #008080">file_exists</span>(<span style="color: #800080">$filename</span>)) { 
<span style="color: #0000ff">echo</span> "The file <span style="color: #800080">$filename</span> exists"<span style="color: #000000">;
} </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
</span><span style="color: #0000ff">echo</span> "The file <span style="color: #800080">$filename</span> does not exist"<span style="color: #000000">;
}
</span>?> 


如果文件存在,执行该 PHP 文件的显示结果是:
The file C:blablaphphello.txt exists.
如果文件不存在,执行该 PHP 文件的显示结果是:
The file C:\blabla\phphello.txt does not exist.
你也可以用file_exists 函数测试某个目录是否存在,示例代码如下:

<span style="color: #0000ff">if</span> (<span style="color: #008080">file_exists</span>("C:\blabla\php"<span style="color: #000000">))
{</span><span style="color: #0000ff">echo</span> "yes"<span style="color: #000000">;}
</span><span style="color: #0000ff">else</span><span style="color: #000000">
{</span><span style="color: #0000ff">echo</span> "no";} 


实例

<span style="color: #008000">/*</span><span style="color: #008000">*
* 文件或目录权限检查函数
*
* @access public
* @param string $file_path 文件路径
* @param bool $rename_prv 是否在检查修改权限时检查执行rename()函数的权限
*
* @return int 返回值的取值范围为{0 <span style="color: #008000">*/</span>
<span style="color: #0000ff">function</span> file_mode_info(<span style="color: #800080">$file_path</span><span style="color: #000000">)
{
</span><span style="color: #008000">/*</span><span style="color: #008000"> 如果不存在,则不可读、不可写、不可改 </span><span style="color: #008000">*/</span>
<span style="color: #0000ff">if</span> (!<span style="color: #008080">file_exists</span>(<span style="color: #800080">$file_path</span><span style="color: #000000">))
{
</span><span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
}
</span><span style="color: #800080">$mark</span> = 0<span style="color: #000000">;
</span><span style="color: #0000ff">if</span> (<span style="color: #008080">strtoupper</span>(<span style="color: #008080">substr</span>(<span style="color: #ff00ff">PHP_OS</span>, 0, 3)) == 'WIN'<span style="color: #000000">)
{
</span><span style="color: #008000">/*</span><span style="color: #008000"> 测试文件 </span><span style="color: #008000">*/</span>
<span style="color: #800080">$test_file</span> = <span style="color: #800080">$file_path</span> . '/cf_test.txt'<span style="color: #000000">;
</span><span style="color: #008000">/*</span><span style="color: #008000"> 如果是目录 </span><span style="color: #008000">*/</span>
<span style="color: #0000ff">if</span> (<span style="color: #008080">is_dir</span>(<span style="color: #800080">$file_path</span><span style="color: #000000">))
{
</span><span style="color: #008000">/*</span><span style="color: #008000"> 检查目录是否可读 </span><span style="color: #008000">*/</span>
<span style="color: #008000">//</span><span style="color: #008000"> http://www.manongjc.com/article/1354.html</span>
<span style="color: #800080">$dir</span> = @<span style="color: #008080">opendir</span>(<span style="color: #800080">$file_path</span><span style="color: #000000">);
</span><span style="color: #0000ff">if</span> (<span style="color: #800080">$dir</span> === <span style="color: #0000ff">false</span><span style="color: #000000">)
{
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$mark</span>; <span style="color: #008000">//</span><span style="color: #008000">如果目录打开失败,直接返回目录不可修改、不可写、不可读</span>
<span style="color: #000000">}
</span><span style="color: #0000ff">if</span> (@<span style="color: #008080">readdir</span>(<span style="color: #800080">$dir</span>) !== <span style="color: #0000ff">false</span><span style="color: #000000">)
{
</span><span style="color: #800080">$mark</span> ^= 1; <span style="color: #008000">//</span><span style="color: #008000">目录可读 001,目录不可读 000</span>
<span style="color: #000000">}
@</span><span style="color: #008080">closedir</span>(<span style="color: #800080">$dir</span><span style="color: #000000">);
</span><span style="color: #008000">/*</span><span style="color: #008000"> 检查目录是否可写 </span><span style="color: #008000">*/</span>
<span style="color: #800080">$fp</span> = @<span style="color: #008080">fopen</span>(<span style="color: #800080">$test_file</span>, 'wb'<span style="color: #000000">);
</span><span style="color: #0000ff">if</span> (<span style="color: #800080">$fp</span> === <span style="color: #0000ff">false</span><span style="color: #000000">)
{
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$mark</span>; <span style="color: #008000">//</span><span style="color: #008000">如果目录中的文件创建失败,返回不可写。</span>
<span style="color: #000000">}
</span><span style="color: #0000ff">if</span> (@<span style="color: #008080">fwrite</span>(<span style="color: #800080">$fp</span>, 'directory access testing.') !== <span style="color: #0000ff">false</span><span style="color: #000000">)
{
</span><span style="color: #008000">//</span><span style="color: #008000"> http://www.manongjc.com/article/1353.html</span>
<span style="color: #800080">$mark</span> ^= 2; <span style="color: #008000">//</span><span style="color: #008000">目录可写可读011,目录可写不可读 010</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">unlink</span>(<span style="color: #800080">$test_file</span><span style="color: #000000">);
</span><span style="color: #008000">/*</span><span style="color: #008000"> 检查目录是否可修改 </span><span style="color: #008000">*/</span>
<span style="color: #800080">$fp</span> = @<span style="color: #008080">fopen</span>(<span style="color: #800080">$test_file</span>, 'ab+'<span style="color: #000000">);
</span><span style="color: #0000ff">if</span> (<span style="color: #800080">$fp</span> === <span style="color: #0000ff">false</span><span style="color: #000000">)
{
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$mark</span><span style="color: #000000">;
}
</span><span style="color: #0000ff">if</span> (@<span style="color: #008080">fwrite</span>(<span style="color: #800080">$fp</span>, "modify test.rn") !== <span style="color: #0000ff">false</span><span style="color: #000000">)
{
</span><span style="color: #800080">$mark</span> ^= 4<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: #008000">/*</span><span style="color: #008000"> 检查目录下是否有执行rename()函数的权限 </span><span style="color: #008000">*/<br></span></span>
// http://www.manongjc.com/article/1352.html
<span style="color: #0000ff">if</span> (@<span style="color: #008080">rename</span>(<span style="color: #800080">$test_file</span>, <span style="color: #800080">$test_file</span>) !== <span style="color: #0000ff">false</span><span style="color: #000000">)
{
</span><span style="color: #800080">$mark</span> ^= 8<span style="color: #000000">;
}
@</span><span style="color: #008080">unlink</span>(<span style="color: #800080">$test_file</span><span style="color: #000000">);
}
</span><span style="color: #008000">/*</span><span style="color: #008000"> 如果是文件 </span><span style="color: #008000">*/</span>
<span style="color: #0000ff">elseif</span> (<span style="color: #008080">is_file</span>(<span style="color: #800080">$file_path</span><span style="color: #000000">))
{
</span><span style="color: #008000">/*</span><span style="color: #008000"> 以读方式打开 </span><span style="color: #008000">*/</span>
<span style="color: #800080">$fp</span> = @<span style="color: #008080">fopen</span>(<span style="color: #800080">$file_path</span>, 'rb'<span style="color: #000000">);
</span><span style="color: #0000ff">if</span> (<span style="color: #800080">$fp</span><span style="color: #000000">)
{
</span><span style="color: #800080">$mark</span> ^= 1; <span style="color: #008000">//</span><span style="color: #008000">可读 001</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: #008000">/*</span><span style="color: #008000"> 试着修改文件 </span><span style="color: #008000">*/</span>
<span style="color: #800080">$fp</span> = @<span style="color: #008080">fopen</span>(<span style="color: #800080">$file_path</span>, 'ab+'<span style="color: #000000">);
</span><span style="color: #0000ff">if</span> (<span style="color: #800080">$fp</span> && @<span style="color: #008080">fwrite</span>(<span style="color: #800080">$fp</span>, '') !== <span style="color: #0000ff">false</span><span style="color: #000000">)
{
</span><span style="color: #800080">$mark</span> ^= 6; <span style="color: #008000">//</span><span style="color: #008000">可修改可写可读 111,不可修改可写可读011...</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: #008000">/*</span><span style="color: #008000"> 检查目录下是否有执行rename()函数的权限 </span><span style="color: #008000">*/</span>
<span style="color: #008000">//</span><span style="color: #008000"> http://www.manongjc.com/article/1355.html</span>
<span style="color: #0000ff">if</span> (@<span style="color: #008080">rename</span>(<span style="color: #800080">$test_file</span>, <span style="color: #800080">$test_file</span>) !== <span style="color: #0000ff">false</span><span style="color: #000000">)
{
</span><span style="color: #800080">$mark</span> ^= 8<span style="color: #000000">;
}
}
}
</span><span style="color: #0000ff">else</span><span style="color: #000000">
{
</span><span style="color: #0000ff">if</span> (@<span style="color: #008080">is_readable</span>(<span style="color: #800080">$file_path</span><span style="color: #000000">))
{
</span><span style="color: #800080">$mark</span> ^= 1<span style="color: #000000">;
}
</span><span style="color: #0000ff">if</span> (@<span style="color: #008080">is_writable</span>(<span style="color: #800080">$file_path</span><span style="color: #000000">))
{
</span><span style="color: #800080">$mark</span> ^= 14<span style="color: #000000">;
}
}
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$mark</span><span style="color: #000000">;
} </span>

 

PHP判断目录是否存在

<span style="color: #008000">/*</span><span style="color: #008000">***************************************************
* 将xml数据流,写入到xml文件
* @param $xmlData
* @return bool|string
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">function</span> writeXmlFile(<span style="color: #800080">$xmlData</span><span style="color: #000000">)
{
</span><span style="color: #800080">$time</span> = <span style="color: #008080">time</span>(); <span style="color: #008000">//</span><span style="color: #008000">获取时间戳,用于给文件命名</span>
<span style="color: #800080">$path</span> = <span style="color: #008080">dirname</span>(<span style="color: #ff00ff">__FILE__</span>); <span style="color: #008000">//</span><span style="color: #008000">获取当前绝对路径</span>
<span style="color: #800080">$path</span> = <span style="color: #008080">substr_replace</span>(<span style="color: #800080">$path</span>, "", <span style="color: #008080">stripos</span>(<span style="color: #800080">$path</span>, "actions\data")); <span style="color: #008000">//</span><span style="color: #008000">将此文件所在的固有路径替换成空</span>
<span style="color: #800080">$path</span> .= "<span style="color: #000000">xmlFiles\"; //存放目录名
/*判断目标目录是否存在,不存在则新建*/
if(!is_dir(</span><span style="color: #800080">$path</span><span style="color: #000000">))
{
mkdir(</span><span style="color: #800080">$path</span><span style="color: #000000">); //新建目录
}
/*记录完整路径和文件名*/
</span><span style="color: #800080">$filePathAndName</span> = <span style="color: #800080">$path</span>.<span style="color: #800080">$time</span>.".xml"<span style="color: #000000">;
/*打开文件,文件名为 + <.xml>*/
// http://www.manongjc.com/article/1356.html
</.xml></span><span style="color: #800080">$fp</span> = fopen(<span style="color: #800080">$filePathAndName</span>, "w"<span style="color: #000000">);
if(!</span><span style="color: #800080">$fp</span><span style="color: #000000">)
{
return false;
}
/*写入文件流*/
</span><span style="color: #800080">$flag</span> = fwrite(<span style="color: #800080">$fp</span>, <span style="color: #800080">$xmlData</span><span style="color: #000000">);
if(!</span><span style="color: #800080">$flag</span><span style="color: #000000">)
{
return false;
}
fclose(</span><span style="color: #800080">$fp</span><span style="color: #000000">);
return </span><span style="color: #800080">$filePathAndName</span><span style="color: #000000">;
} </span>

 

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
Artikel sebelumnya:php创建多级目录的两种方法Artikel seterusnya:PHP如何获取文件行数