파일이나 디렉터리가 있는지 확인하려면 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 파일을 실행하여 표시되는 결과는 다음과 같습니다.
C:blablaphello.txt 파일이 존재합니다.
파일이 존재하지 않는 경우 PHP를 실행하여 표시되는 결과입니다. 파일은 다음과 같습니다.
C:blablaphello.txt 파일이 존재하지 않습니다.
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>