多个图像IF file_exists查询

时间:2014-03-24 02:22:16

标签: php

我正在尝试执行多个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.
<? } ?>

2 个答案:

答案 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.";