To check whether a file or directory exists, we use the commonly used function file_exists in PHP. This function can achieve the function I want. Please refer to it slowly below
The following is a simple example code for checking whether a 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>?>
If the file exists, the displayed result of executing the PHP file is:
The file C:blablaphphello.txt exists.
If the file does not exist, the displayed result of executing the PHP file is:
The file C:blablaphphello.txt does not exist.
You can also use the file_exists function to test whether a directory exists. The sample code is as follows:
<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";}
Example
<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 determines whether the directory exists
<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>