首頁 > php教程 > php手册 > 主體

php中檢查檔案或目錄是否存在的程式碼小結

WBOY
發布: 2016-10-28 15:03:28
原創
1122 人瀏覽過

檢查檔案或目錄是否存在 ,我們使用了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:blablaphphello. 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 <= x <= 15},每个值表示的含义可由四位二进制数组合推出。
* 返回值在二进制计数法中,四位由高到低分别代表
* 可执行rename()函数权限、可对文件追加内容权限、可写入文件权限、可读取文件权限。
</span><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>
登入後複製
// 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
</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>
登入後複製

 

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板