在我的应用程序中有一个文件夹名称视频.我想raed所有文件只有这个文件夹我几乎没有在文件夹视频中显示任何文件夹除了.DS_Store目前我已申请if条件隐藏这个但我想要一个完美的解决方案可以任何一个帮助: - 我的代码是:----
$dir = "../videos/";
$dh = opendir($dir);
print '<select size="4" name="vidfile">';
while (($file = readdir($dh)) !== false) {
if (is_file($dir.'/'.$file) && $file != "." && $file != "..") {
if ($file!=".DS_Store") {
print "<option>{$file}</option>";
}
}
}
print '</select>';
closedir($dh);
Run Code Online (Sandbox Code Playgroud)
Gor*_*don 13
快速简便的解决方案
您可以让您的生活更轻松,并使用a DirectoryIterator来浏览目录.
echo '<select name="vids" size="4">';
foreach( new DirectoryIterator('/path/to/videos') as $file) {
if( $file->isFile() === TRUE && $file->getBasename() !== '.DS_Store') {
printf("<option>%s</option>\n", htmlentities($file->getBasename()));
}
}
echo '</select>';
Run Code Online (Sandbox Code Playgroud)
改进:从SelectBox构建中解耦目录过滤
如果要将过滤逻辑与foreach循环分离,可以将a子类FilterIterator化为将该逻辑封装到其accept()方法中.在DirectoryIterator必须被包裹成FilterIterator即可.重点当然是可重用性:
class MyFilter extends FilterIterator
{
public function accept()
{
return $this->current()->isFile() === TRUE &&
$this->current()->getBasename() !== '.DS_Store';
}
}
$iterator = new MyFilter(new DirectoryIterator('/path/to/videos'));
Run Code Online (Sandbox Code Playgroud)
在foreach过滤的迭代器上使用时,它会accept()自动触发.如果accept()返回FALSE,则在迭代中过滤掉当前元素.
您可以像这样创建SelectBox:
echo '<select name="vids" size="4">';
foreach( $iterator as $file) {
printf("<option>%s</option>\n", htmlentities($file->getBasename()));
}
echo '</select>';
Run Code Online (Sandbox Code Playgroud)
替代子类化FilterIterator
如果你懒得写一个单独的FilterIterator或认为它不值得为特定的情况或已经有某个地方的验证器并且不想复制他们的代码,但仍然想要解耦Filtering和SelectBox创建,你也可以使用这个自定义FilterChainIterator和添加回调:
$iterator = new FilterChainIterator(new DirectoryIterator('/path/to/videos'));
$iterator->addCallback(function($file) {
return $file->isFile() === TRUE &&
$file->getBasename() !== '.DS_Store';});
Run Code Online (Sandbox Code Playgroud)
SelectBox创建与上面显示的相同.
改进:使SelectBox创建可重用
此外,如果要使SelectBox创建可重用,为什么不为它创建一个Helper.下面是一个非常简单的使用DOM来创建实际的HTML.您传入任何迭代器,当您调用它的render()方法或在字符串上下文中使用它时,它将为您创建HTML :
class SelectBox
{
protected $iterator;
public function __construct(Iterator $iterator)
{
$this->iterator = $iterator;
}
public function render()
{
$dom = new DOMDocument;
$dom->formatOutput = TRUE;
$dom->loadXml('<select name="vids"/>');
$dom->documentElement->appendChild(new DOMElement('option', 'Pick One'));
foreach($this->iterator as $option) {
$dom->documentElement->appendChild(
new DOMElement('option', $option));
}
return $dom->saveXml($dom->documentElement);
}
public function __toString()
{
return $this->render();
}
}
Run Code Online (Sandbox Code Playgroud)
然后从迭代器中打印SelectBox非常简单
echo new SelectBox(new MyFilter(new DirectoryIterator('/path/to/videos')));
Run Code Online (Sandbox Code Playgroud)
这是非常灵活的,因为有所有的迭代器.例如
echo new SelectBox(new ArrayIterator(array('foo', 'bar', 'baz')));
Run Code Online (Sandbox Code Playgroud)
会给出一个整齐的格式
<select>
<option>Pick One</option>
<option>foo</option>
<option>bar</option>
<option>baz</option>
</select>
Run Code Online (Sandbox Code Playgroud)