file_exists() - 检查文件或目录是否存在 - php 文件目录函数
file_exists()
(PHP 4, PHP 5, PHP 7)
检查文件或目录是否存在
说明
file_exists(string $filename): bool检查文件或目录是否存在。
参数
$filename文件或目录的路径。
在 Windows 中要用//computername/share/filename或者computernamesharefilename来检查网络中的共享文件。
返回值
如果由$filename指定的文件或目录存在则返回TRUE
,否则返回FALSE
。
This function will returnFALSE
for symlinks pointing to non-existing files.Warning
如果因为安全模式的限制而导致不能访问文件的话,该函数会返回FALSE
。然而,可以使用include来包含,如果文件在safe_mode_include_dir所指定的目录里。
The check is done using the real UID/GID instead of the effective one.
Note:因为 PHP 的整数类型是有符号整型而且很多平台使用 32 位整型,对 2GB以上的文件,一些文件系统函数可能返回无法预期的结果。
范例
测试一个文件是否存在
错误/异常
失败时抛出E_WARNING
警告。
注释
Note:此函数的结果会被缓存。参见clearstatcache()以获得更多细节。
Tip自 PHP 5.0.0 起,此函数也用于某些URL 包装器。请参见支持的协议和封装协议以获得支持stat()系列函数功能的包装器列表。
参见
is_readable()
判断给定文件名是否可读is_writable()
判断给定的文件名是否可写is_file()
判断给定文件名是否为一个正常的文件file()
把整个文件读入一个数组中
Note: The results of this function are cached. See clearstatcache() for more details. That's a pretty big note. Don't forget this one, since it can make your file_exists() behave unexpectedly - probably at production time ;)
Note that realpath() will return false if the file doesn't exist. So if you're going to absolutize the path and resolve symlinks anyway, you can just check the return value from realpath() instead of calling file_exists() first
I needed to measure performance for a project, so I did a simple test with one million file_exists() and is_file() checks. In one scenario, only seven of the files existed. In the second, all files existed. is_file() needed 3.0 for scenario one and 3.3 seconds for scenario two. file_exists() needed 2.8 and 2.9 seconds, respectively. The absolute numbers are off course system-dependant, but it clearly indicates that file_exists() is faster.
In response to seejohnrun's version to check if a URL exists. Even if the file doesn't exist you're still going to get 404 headers. You can still use get_headers if you don't have the option of using CURL.. $file = 'http://www.domain.com/somefile.jpg'; $file_headers = @get_headers($file); if($file_headers[0] == 'HTTP/1.1 404 Not Found') { $exists = false; } else { $exists = true; }
I wrote this little handy function to check if an image exists in a directory, and if so, return a filename which doesnt exists e.g. if you try 'flower.jpg' and it exists, then it tries 'flower[1].jpg' and if that one exists it tries 'flower[2].jpg' and so on. It works fine at my place. Ofcourse you can use it also for other filetypes than images.
If you are trying to access a Windows Network Share you have to configure your WebServer with enough permissions for example: $file = fopen("\\siscomx17\c\websapp.log",'r'); You will get an error telling you that the pathname doesnt exist this will be because Apache or IIS run as LocalSystem so you will have to enter to Services and configure Apache on "Open a session as" Create a new user that has enough permissions and also be sure that target share has the proper permissions. Hope this save some hours of research to anyone.
here a function to check if a certain URL exist: in my CMS, I am using it with those lines:
With PHP 7.0 on Ubuntu 17.04 and with the option allow_url_fopen=On, file_exists() returns always false when trying to check a remote file via HTTP. So $url="http://www.somewhere.org/index.htm"; if (file_exists($url)) echo "Wow!\n"; else echo "missing\n"; returns always "missing", even for an existing URL. I found that in the same situation the file() function can read the remote file, so I changed my routine in $url="http://www.somewhere.org/index.htm"; if (false!==file($url)) echo "Wow!\n"; else echo "missing\n"; This is clearly a bit slower, especially if the remote file is big, but it solves this little problem.
When using file_exists, seems you cannot do: so you must do: Then things will work fine. This is at least the case on this Windows system running php 5.2.5 and apache 2.2.3 Not sure if it is down to the concatenation or the fact theres a constant in there, i'm about to run away and test just that...
file_exists will have trouble finding your file if the file permissions are not read enabled for 'other' when not owned by your php user. I thought I was having trouble with a directory name having a space in it (/users/andrew/Pictures/iPhoto Library/AlbumData.xml) but the reality was that there weren't read permissions on Pictures, iPhoto Library or AlbumData.xml. Once I fixed that, file_exists worked.
For some reason, none of the url_exists() functions posted here worked for me, so here is my own tweaked version of it.
this code here is in case you want to check if a file exists in another server: unfortunately the file_exists can't reach remote servers, so I used the fopen function.
Note on openspecies entry (excellent btw, thanks!). If your server cannot resolve its own DNS, use the following: $f = preg_replace('/www\.yourserver\.(net|com)/', getenv('SERVER_ADDR'), $f); Just before the $h = @get_headers($f); line. Replace the extensions (net|com|...) in the regexp expression as appropriate. EXAMPLE: File you are checking for: http://www.youserver.net/myfile.gif Server IP: 10.0.0.125 The preg_replace will effectively 'resolve' the address for you by assigning $f as follows: http://10.0.0.125/myfile.gif
If the file being tested by file_exists() is a file on a symbolically-linked directory structure, the results depend on the permissions of the directory tree node underneath the linked tree. PHP under a web server (i.e. apache) will respect permissions of the file system underneath the symbolic link, contrasting with PHP as a shell script which respects permissions of the directories that are linked (i.e. on top, and visible). This results in files that appear to NOT exist on a symbolic link, even though they are very much in existance and indeed are readable by the web server.
Note that this will return false for streams, eg, php://stdin.
file_exists() does NOT search the php include_path for your file, so don't use it before trying to include or require. use @$result = include $filename; Yes, include does return false when the file can't be found, but it does also generate a warning. That's why you need the @. Don't try to get around the warning issue by using file_exists(). That will leave you scratching your head until you figure out or stumble across the fact that file_exists() DOESN'T SEARCH THE PHP INCLUDE_PATH.
Here is a simpler version of url_exists:
I made a bit of code that sees whether a file served via RTSP is there or not:
file_exists() is vulnerable to race conditions and clearstatcache() is not adequate to avoid it. The following function is a good solution: SEE ALSO: https://linux.die.net/man/2/open about O_CREAT|O_EXCL (which is used with the 'x' modifier for fopen()) and problems with NFS
Older php (v4.x) do not work with get_headers() function. So I made this one and working.
I spent the last two hours wondering what was wrong with my if statement: file_exists($file) was returning false, however I could call include($file) with no problem. It turns out that I didn't realize that the php include_path value I had set in the .htaccess file didn't carry over to file_exists, is_file, etc. Thus:
file_exists() will return FALSE for broken links $ ln -s does_not_exist my_link $ ls -l lrwxr-xr-x 1 user group 14 May 13 17:28 my_link -> does_not_exist $ php -r "var_dump(file_exists('my_link'));" bool(false)
If checking for a file newly created by an external program in Windows then file_exists() does not recognize it immediately. Iy seems that a short timeout may be required.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!