Php 读取文件夹中的文件
在我的应用程序中有一个文件夹名“视频”。我只想raed这个文件夹的所有文件,我几乎没有这样做,它不会在文件夹视频中显示任何文件夹,除了.DS_存储 目前我已经应用了if条件来隐藏这个,但我想要一个完美的解决方案,任何人都可以帮助:-我的代码是:----Php 读取文件夹中的文件,php,file,Php,File,在我的应用程序中有一个文件夹名“视频”。我只想raed这个文件夹的所有文件,我几乎没有这样做,它不会在文件夹视频中显示任何文件夹,除了.DS_存储 目前我已经应用了if条件来隐藏这个,但我想要一个完美的解决方案,任何人都可以帮助:-我的代码是:---- $dir=“../videos/”; $dh=opendir($dir); 打印“”; while(($file=readdir($dh))!==false){ 如果(is_file($dir./'.$file)&&&$file!=“&&&$fi
$dir=“../videos/”;
$dh=opendir($dir);
打印“”;
while(($file=readdir($dh))!==false){
如果(is_file($dir./'.$file)&&&$file!=“&&&$file!=”){
如果($file!=“.DS_存储”){
打印“{$file}”;
}
}
}
打印“”;
closedir($dh);
首先,如果opendir失败,不要继续使用垃圾邮件。它忽略非文件(目录)和以点开头的文件(包括.,..,.htaccess等)这里有一个变体,它使用一个排除的文件数组,收集所有
选项
元素,然后将它们作为一个元素输出。另外,glob()
允许通过其大括号语法过滤某些文件扩展名,如注释行外所示
$dir = "../videos";
$mask = "*";
// $mask = "*.{wmv,mpeg,mpg}"; // get only files of these types
$files = glob($dir."/".$mask);
$excluded_files = array(".DS_store");
$html = null;
if (count($files) > 0)
foreach ($files as $file)
{
$filename = basename($file);
if (in_array($filename, $excluded_files)) continue;
$html.="<option>".htmlentities($filename)."</option>";
}
echo $html;
$dir=“../videos”;
$mask=“*”;
//$mask=“*.{wmv,mpeg,mpg}”//仅获取这些类型的文件
$files=glob($dir.“/”$mask);
$excluded_files=数组(“.DS_存储”);
$html=null;
如果(计数($files)>0)
foreach($files作为$file)
{
$filename=basename($file);
如果(在_数组中($filename,$excluded_files))继续;
$html.=''.htmlentities($filename.);
}
echo$html;
只要如果($file[0]==”)继续代码>而不是这两个if可能是您正在寻找的。
虽然我会让它有点不同,分为两部分。
获取数据部分:
<?php
$dir = "../videos/";
$dh = opendir($dir);
$filesarr = array();
while (($file = readdir($dh)) !== false) {
if ($file[0] != ".") $filesarr[] = $file;
}
closedir($dh);
?>
<select size="4" name="vidfile">
<?php foreach ($filesarr as $file): ?>
<option><?php echo $file ?></option>
<?php endforeach ?>
</select>
和显示数据部分:
<?php
$dir = "../videos/";
$dh = opendir($dir);
$filesarr = array();
while (($file = readdir($dh)) !== false) {
if ($file[0] != ".") $filesarr[] = $file;
}
closedir($dh);
?>
<select size="4" name="vidfile">
<?php foreach ($filesarr as $file): ?>
<option><?php echo $file ?></option>
<?php endforeach ?>
</select>
对我来说,快速简便的解决方案似乎更干净
你可以让你的生活更轻松,用一个简单的方法来浏览目录
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>';
在过滤迭代器上使用foreach
时,它将自动触发accept()
。如果accept()
返回FALSE
,则当前元素将在迭代中过滤掉
您可以这样创建选择框,然后:
echo '<select name="vids" size="4">';
foreach( $iterator as $file) {
printf("<option>%s</option>\n", htmlentities($file->getBasename()));
}
echo '</select>';
SelectBox的创建与上面显示的相同
改进:使SelectBox创建可重用
此外,如果要使SelectBox创建可重用,为什么不为其创建一个帮助器呢。下面是一个非常简单的例子,它使用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();
}
}
考虑到每件事都有迭代器,这是非常灵活的。比如说
echo new SelectBox(new ArrayIterator(array('foo', 'bar', 'baz')));
将给出一个格式整齐的
<select>
<option>Pick One</option>
<option>foo</option>
<option>bar</option>
<option>baz</option>
</select>
挑一个
福
酒吧
巴兹
为什么在$file->isFile()===TRUE
中使用相同的(==)运算符?我可以想象,$file->isFile()
也能起到同样的作用。
echo new SelectBox(new MyFilter(new DirectoryIterator('/path/to/videos')));
echo new SelectBox(new ArrayIterator(array('foo', 'bar', 'baz')));
<select>
<option>Pick One</option>
<option>foo</option>
<option>bar</option>
<option>baz</option>
</select>