image-file

How to get only images using scandir in PHP?

♀尐吖头ヾ 提交于 2019-11-26 20:16:40
Is there any way to get only images with extensions jpeg , png , gif etc while using $dir = '/tmp'; $files1 = scandir($dir); You can use glob $images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE); If you need this to be case-insensitive, you could use a DirectoryIterator in combination with a RegexIterator or pass the result of scandir to array_map and use a callback that filters any unwanted extensions. Whether you use strpos , fnmatch or pathinfo to get the extension is up to you. Rajesh Kumar R The actual question was using scandir and the answers end up in glob. There is a huge difference in

How to get only images using scandir in PHP?

試著忘記壹切 提交于 2019-11-26 07:33:04
问题 Is there any way to get only images with extensions jpeg , png , gif etc while using $dir = \'/tmp\'; $files1 = scandir($dir); 回答1: You can use glob $images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE); If you need this to be case-insensitive, you could use a DirectoryIterator in combination with a RegexIterator or pass the result of scandir to array_map and use a callback that filters any unwanted extensions. Whether you use strpos , fnmatch or pathinfo to get the extension is up to you. 回答2: