我正在尝试执行多个if file_exists查询,我似乎无法正确使用它。任何帮助将不胜感激。
<? if (file_exists("images/one.jpg" || "images/two.jpg")) { ?>
Yes, there is either file one.jpg OR file two.jpg OR both are present.
<? } else { ?>
No dice, Neither files are there.
<? } ?>
答案 0 :(得分:4)
您需要为不同的文件分隔file_exists
次调用:
<? if (file_exists("images/one.jpg") || file_exists("images/two.jpg")) { ?>
Yes, there is either file one.jpg OR file two.jpg OR both are present.
<? } else { ?>
No dice, Neither files are there.
<? } ?>
答案 1 :(得分:1)
如果要检测更多文件,使用太多||
运算符会很难看。您最好将它们存储在一个数组中,然后使用foreach
来测试是否存在任何一个。
$files = array("images/ong.jpg", "images/two.jpg");
$exists = false;
foreach($files as $file)
{
if(file_exists($file)) {
$exists = true;
break;
}
}
echo exists ? "Yes, there is either file one.jpg OR file two.jpg OR both are present." : "No dice, Neither files are there.";