1

I want to check multiple files at once if they exist or not. I want to show a text if any of these files exists:

$filepre = ''.$br.'/filethumb/'.$name.'.jpg';
$filess = ''.$br.'/ss/'.$name.'.jpg';
$filess1 = ''.$br.'/ss/'.$name.'1.jpg';
$filess2 = ''.$br.'/ss/'.$name.'2.jpg';
$filess3 = ''.$br.'/ss/'.$name.'3.jpg';
$filess4 = ''.$br.'/ss/'.$name.'4.jpg';
$filess5 = ''.$br.'/ss/'.$name.'5.jpg';
$filess6 = ''.$br.'/ss/'.$name.'6.jpg';
$filess7 = ''.$br.'/ss/'.$name.'7.jpg';
$filess8 = ''.$br.'/ss/'.$name.'8.jpg';
$filess9 = ''.$br.'/ss/'.$name.'9.jpg';

Then I want to check if any of these files exist or not. If 1 of these 10 files exists then show a text "Screenshots:". If no file exists then nothing will be shown.

I tried:

if (file_exists($filess or $filess1 or $filess2 or $filess3 or $filess4 or $filess5 or $filess6 or $filess7 or $filess8 or $filess9)){
echo 'Screenshots';
}

Did I do anything wrong that it's not working?

2
  • Put your filenames in an array and loop through it. Commented Feb 8, 2017 at 16:32
  • how? can you help me in this by write down? Commented Feb 8, 2017 at 16:33

3 Answers 3

7

Put your filenames in an array and loop through:

$files = array('file_name_1', 'file_name_2'...);

foreach($files as $file){
    if(file_exists($file)){
        echo "screenshots";
        break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

I would use an array, filter it and check if it is empty or not:

$files[] = $br.'/ss/'.$name.'1.jpg';
$files[] = $br.'/ss/'.$name.'2.jpg';
//etc...

if(array_filter($files, 'file_exists')) {
    echo "Screenshots:";
}

This is extendable as you can compare the count() of the result to see if 2, 3, 4, etc.. exist or if the count equals the count of the original array.

Comments

1

You need to rearrange the checking in the if condition,

if (file_exists($filess) or file_exists($filess1) or file_exists($filess2) or file_exists($filess3) or file_exists($filess4) or file_exists($filess5) or file_exists($filess6) or file_exists($filess7) or file_exists($filess8) or file_exists($filess9)){
    echo 'Screenshots';
}

Only if you don't want to use array and looping.

1 Comment

thanks for your answer, that is what i am looking for and its working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.