Pap*_*ase 93 php file-io image file
我需要查看我的cdn上是否存在特定图像.
我尝试过以下内容并不起作用:
if (file_exists(http://www.example.com/images/$filename)) {
echo "The file exists";
} else {
echo "The file does not exist";
}
Run Code Online (Sandbox Code Playgroud)
即使图像存在或不存在,它总是说"文件存在".我不确定为什么它不起作用......
kni*_*ttl 121
您至少需要引号中的文件名(作为字符串):
if (file_exists('http://www.mydomain.com/images/'.$filename)) {
… }
Run Code Online (Sandbox Code Playgroud)
此外,请确保$filename已正确验证.然后,只有allow_url_fopen在PHP配置中激活它才会起作用
Jef*_*son 104
if (file_exists('http://www.mydomain.com/images/'.$filename)) {}
Run Code Online (Sandbox Code Playgroud)
这不适合我.我这样做的方式是使用getimagesize.
$src = 'http://www.mydomain.com/images/'.$filename;
if (@getimagesize($src)) {
Run Code Online (Sandbox Code Playgroud)
请注意,'@'表示如果图像不存在(在这种情况下函数通常会抛出错误:) getimagesize(http://www.mydomain.com/images/filename.png) [function.getimagesize]: failed,它将返回false.
Riz*_*ero 14
好吧,file_exists不说文件是否存在,它说是否存在路径.⚡⚡⚡⚡⚡⚡⚡
因此,要检查它是否是文件,那么您应该is_file一起使用 file_exists以了解路径后面是否确实存在文件,否则file_exists将返回true任何现有路径.
这是我使用的功能:
function fileExists($filePath)
{
return is_file($filePath) && file_exists($filePath);
}
Run Code Online (Sandbox Code Playgroud)
pin*_*sai 12
试试这样:
$file = '/path/to/foo.txt'; // 'images/'.$file (physical path)
if (file_exists($file)) {
echo "The file $file exists";
} else {
echo "The file $file does not exist";
}
Run Code Online (Sandbox Code Playgroud)
小智 9
以下是检查文件是否存在的最简单方法:
if(is_file($filename)){
return true; //the file exist
}else{
return false; //the file does not exist
}
Run Code Online (Sandbox Code Playgroud)
首先要了解的事情是:你没有文件.
文件是文件系统的主题,但是您使用HTTP协议发出请求,该协议不支持URL而是文件.
因此,您必须使用浏览器请求未存在的文件并查看响应代码.如果它不是404,你就无法使用任何包装器来查看文件是否存在而你必须使用其他协议请求你的cdn,例如FTP
public static function is_file_url_exists($url) {
if (@file_get_contents($url, 0, NULL, 0, 1)) {
return 1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果该文件位于您的本地域中,则无需输入完整的URL.只有文件的路径.如果文件位于不同的目录中,则需要在路径前加上".".
$file = './images/image.jpg';
if (file_exists($file)) {}
Run Code Online (Sandbox Code Playgroud)
通常是"." 保持关闭将导致文件显示为不存在,实际上它确实存在.