定义和用法
file_exists() 函数检查文件或目录是否存在。
如果指定的文件或目录存在则返回 true,否则返回 false。
exists中文翻译为存在的意思。
语法file_exists(path)
例子
Example #1<?phpecho file_exists("test.txt");?>
输出:1
Example #2<?php $filename = '/filepath/filename.txt'; if (file_exists($filename)) { echo "The file $filename exists";} else { echo "The file $filename does not exist";}?>
输出:(如果文件存在的话)echo "The file /filepath/filename.txt does not exist";
上面的提示是找不到文件,为什么?
其实file_exist要用绝对路径
解决办法:
1、要不在"filepath/filename.txt"前面加"."表示当前根目录下的filepath文件夹
2、要不在"filepath/filename.txt"前面加上完整路径,如:D:/www/filepath/filename.txt
以上函数常常与unlink函数一起使用
思路:通过函数file_exists判断文件是否存在,然后再通过unlink删除响应的文件。
注: 本函数的结果会被缓存。更多信息参见 clearstatcache().
警告: 如果因为安全模式的限制而导致不能访问文件的话,该函数会返回 FALSE。然而,如果文件在 safe_mode_include_dir 所指定的目录内的话,仍然可以使用 include 来包含。
本文出自:琅枫个人博客。如需转载请注明出处!
本文出处:"https://www.phpfeng.cn/function/255.html"