How to check if something is a file or a directory within an array in PHP?

General Tech Bugs & Fixes 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am trying to filter out some folders in this array:

Array
(
    [2] => 20151113_152308.jpg
    [3] => 20160524_173033.jpg
    [4] => 20160810_100405.jpg
    [5] => 20160816_142932.jpg
    [6] => 20160817_135507.jpg
    [7] => 20170203_145901.jpg
    [8] => 20170203_151235.jpg
    [9] => Afbeelding-ifone-131.jpg
    [10] => Afbeelding-ifone-508.jpg
    [11] => Bestratingswerkzaamheden
    [12] => Mvc-161.jpg
    [13] => Rioleringswerkzaamheden
    [14] => SANY0016.jpg
    [15] => SANY0036.jpg
    [16] => SANY0060.jpg
    [17] => SANY0132.jpg
    [18] => SANY0133.jpg
    [19] => SANY0158.jpg
    [20] => SANY0159.jpg
    [21] => SANY0361.jpg
    [22] => Zeefwerkzaamheden
)

As you can see there are files in the array but also some folders (no extension). I've tried filtering them out using is_dir().

Using the following code:

PHP
//Base path
$pad = "/home/studione/public_html/_extern/website/cms/images/beeldbank/".$alias."";
//Get everything from above path
$directories = array_diff(scandir($pad), array('.', '..', 'index.html'));
// Print array of above
echo '
';
print_r($directories);
echo '
'
; foreach($directories as $image){ if(!is_dir($image){ $impressieslider .= '

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.