PHP:如何检查图像文件是否存在? [英] PHP: How to check if image file exists?

查看:114
本文介绍了PHP:如何检查图像文件是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经尝试过以下操作,但它不起作用:

  if(file_exists(http://www.example.com/images/$filename)){
echo该文件存在;
} else {
echo该文件不存在;
}

即使图像存在或不存在,它总是说文件已存在。我不知道为什么它不工作...

解决方案

您至少需要引号中的文件名(作为字符串) :

  if(file_exists('http://www.mydomain.com/images/'.$filename)){
...}

另外,请确保 $ filename 被正确验证。然后,只有在 allow_url_fopen 在您的PHP配置


中激活

I need to see if a specific image exists on my cdn.

I've tried the following and it doesn't work:

if (file_exists(http://www.example.com/images/$filename)) {
    echo "The file exists";
} else {
    echo "The file does not exist";
}

Even if the image exists or doesn't exist, it always says "The file exists". I'm not sure why its not working...

解决方案

You need the filename in quotation marks at least (as string):

if (file_exists('http://www.mydomain.com/images/'.$filename)) {
 … }

Also, make sure $filename is properly validated. And then, it will only work when allow_url_fopen is activated in your PHP config

这篇关于PHP:如何检查图像文件是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆