for($index=0; $index < $indexCount; $index++) {
if (substr($dirArray[$index], 0, 1) != "."
&& strtolower(substr($dirArray[$index], -3)) == 'png'
&& strtolower(substr($dirArray[$index], -3)) == 'jpg')
echo '<option value="'.$dirArray[$index].'">'.$dirArray[$index].'</option>';
}
这应该可行,但有更优雅的解决方案,比如使用DirectoryIterator
(见这里):
foreach (new DirectoryIterator('your_directory') as $fileInfo) {
if($fileInfo->isDot()
|| !in_array($fileInfo->getExtension(), array('png', 'jpg')))
continue;
echo sprintf('<option value="%s">%s</option>',
$fileInfo->getFilename(),
$fileInfo->getFilename());
}
代码未经测试,您可能需要对其进行一些修改。