判断目录或者文件是否存在,需要了解三个函数file_exists(),is_file(),is_dir()。
file_exists 判断文件是否存在或者是目录是否存在;
is_file 只判断文件是否存在;
is_dir 判断目录是否存在;
检查文件或目录是否存在。
index.php文件
<html> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>目录/文件是否存在-炫代码</title></head> <body> <table> <form action="index.php" method="post" enctype="multipart/form-data" name="form1"> <tr class="firstRow"> <td width="120" valign="top"></td> <td width="120" valign="top"></td> <td width="120" valign="top"></td> <td width="120" valign="top"></td> </tr> <tr> <td width="120" valign="top"></td> <td width="120" valign="top" style="word-break: break-all;"> 文件或目录名称: </td> <td width="120" valign="top"><input name="file_name" type="text" id="file_name" size="35"></td> <td width="120" valign="top"><input type="submit" name="Sub" value="提交"></td> </tr> <tr> <td width="120" valign="top"></td> <td width="120" valign="top"></td> <td width="120" valign="top"><span style="color: #FF0000">请输入正确的目录/文件路径!例如:D:\XXX\XXX.txt</span></td> <td width="120" valign="top"></td> </tr> </form> <?php if (isset($_POST ['file_name'])!= "") { $file_name = $_POST ['file_name'] ; if (file_exists ( $file_name )) { echo "<script>alert('目录/文件是存在!');</script>"; } else { echo "<script>alert('目录/文件不存在!');</script>"; } } else { echo "<script>alert('请输入正确的目录、文件路径!');</script>"; } ?> </table></body></html>
is_file判断文件是否存在,is_dir判断目录是否存在
index.php文件
<html> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>目录/文件是否存在-炫代码</title></head> <body> <table> <form action="index.php" method="post" enctype="multipart/form-data" name="form1"> <tr class="firstRow"> <td width="120" valign="top"></td> <td width="120" valign="top"></td> <td width="120" valign="top"></td> <td width="120" valign="top"></td> </tr> <form action="index.php" method="post" enctype="multipart/form-data" name="form1"> <tr> <td width="120" valign="top"></td> <td width="120" valign="top" style="word-break: break-all;"> 目录名称: </td> <td width="120" valign="top"><input name="path_name" type="text" id="path_name" size="35"></td> <td width="120" valign="top"><input type="submit" name="Sub" value="目录检测"> </td> </tr> </form> <form action="index.php" method="post" enctype="multipart/form-data" name="form2"> <tr> <td width="120" valign="top"></td> <td width="120" valign="top" style="word-break: break-all;"> 文件名称: </td> <td width="120" valign="top"><input name="file_name" type="text" id="file_name" size="35"></td> <td width="120" valign="top"><input type="submit" name="Sub" value="文件检测"></td> </tr> <tr> <td width="120" valign="top"></td> <td width="120" valign="top"></td> <td width="120" valign="top"><span style="color: #FF0000">请输入正确的目录/文件路径!例如:D:\XXX\XXX.txt</span></td> <td width="120" valign="top"></td> </tr> </form> <?php if ($_POST['Sub']=='目录检测') { if (isset($_POST ['path_name'])!= "") { $path_name = $_POST ['path_name'] ; if (is_dir( $path_name )) { echo "<script>alert('目录是存在!');</script>"; } else { echo "<script>alert('目录不存在!');</script>"; } } else { echo "<script>alert('请输入目录路径!');</script>"; } } else { if (isset($_POST ['file_name'])!= "") { $file_name = $_POST ['file_name'] ; if (is_file( $file_name )) { echo "<script>alert('文件是存在!');</script>"; } else { echo "<script>alert('文件不存在!');</script>"; } } else { echo "<script>alert('请输入文件路径!');</script>"; } } ?> </table></body></html>
文件存在的情况下,is_file比file_exists要快得多,要检测文件所在的目录越深,速度差越多,但至少快4倍。
文件不存在的情况下,is_file比file_exists要慢一点点,但可以忽略不计。
目录存在的情况下,is_dir比file_exists要快得多;
目录不存在的情况下,is_dir比file_exists要慢一点点,但可以忽略不计。
如果要判断文件是否存在,用函数 is_file(),
如果要判断目录是否存在,用函数 is_dir(),
不确定传入的参数是文件还是目录的时候file_exists()