php 检查文件是否存在于php中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4253487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
check if file exists in php
提问by anonymous
if (!(file_exists(http://mysite.com/images/thumbnail_1286954822.jpg))) {
$filefound = '0';
}
why won't this work?
为什么这行不通?
回答by Haim Evgi
if (!file_exists('http://example.com/images/thumbnail_1286954822.jpg')) {
$filefound = '0';
}
回答by Ignacio Vazquez-Abrams
The function expects a string.
file_exists()
does not work properly with HTTP URLs.
该函数需要一个字符串。
file_exists()
HTTP URL 不能正常工作。
回答by IRSHAD
file_existschecks whether a file exist in the specified path or not.
file_exists检查指定路径中是否存在文件。
Syntax:
句法:
file_exists ( string $filename )
Returns TRUE
if the file or directory specified by filename exists; FALSE
otherwise.
返回TRUE
文件名指定的文件或目录是否存在;FALSE
除此以外。
$filename = BASE_DIR."images/a/test.jpg";
if (file_exists($filename)){
echo "File exist.";
}else{
echo "File does not exist.";
}
Another alternative method you can use getimagesize(), it will return 0(zero) if file/directory is not available in the specified path.
您可以使用 getimagesize() 的另一种替代方法,如果指定路径中的文件/目录不可用,它将返回 0(零)。
if (@getimagesize($filename)) {...}
回答by JJJ
Based on your comment to Haim, is this a file on your own server? If so, you need to use the file system path, not url (e.g. file_exists( '/path/to/images/thumbnail.jpg' )
).
根据您对 Haim 的评论,这是您自己服务器上的文件吗?如果是这样,您需要使用文件系统路径,而不是 url(例如file_exists( '/path/to/images/thumbnail.jpg' )
)。
回答by Nashir
for me also the file_exists() function is not working properly. So I got this alternative solution. Hope this one help someone
对我来说,file_exists() 函数也不能正常工作。所以我得到了这个替代解决方案。希望这对某人有所帮助
$path = 'http://localhost/admin/public/upload/video_thumbnail/thumbnail_1564385519_0.png';
if (@GetImageSize($path)) {
echo 'File exits';
} else {
echo "File doesn't exits";
}
回答by Pradeep Kumar Prabaharan
You can also use PHP
get_headers()function.
您还可以使用PHP
get_headers()函数。
Example:
例子:
function check_file_exists_here($url){
$result=get_headers($url);
return stripos($result[0],"200 OK")?true:false; //check if $result[0] has 200 OK
}
if(check_file_exists_here("http://www.mywebsite.com/file.pdf"))
echo "This file exists";
else
echo "This file does not exist";