How to get only images using scandir in PHP?

后端 未结 6 1328
逝去的感伤
逝去的感伤 2020-11-27 15:52

Is there any way to get only images with extensions jpeg, png, gif etc while using

$dir    = \'/tmp\';
$files1 = scand         


        
6条回答
  •  孤独总比滥情好
    2020-11-27 16:20

    If you would like to scan a directory and return filenames only you can use this:

    $fileNames = array_map(
        function($filePath) {
            return basename($filePath);
        },
        glob('./includes/*.{php}', GLOB_BRACE)
    );
    

    scandir() will return . and .. as well as the files, so the above code is cleaner if you just need filenames or you would like to do other things with the actual filepaths

提交回复
热议问题