2010-08-25 170 views
3

在我的應用程序中有一個文件夾名稱視頻。我想raed所有文件只有這個文件夾我已經做了差不多它不顯示任何文件夾中的文件夾視頻除.DS_Store 目前我已經申請如果條件隱藏這個,但我想要一個完美的解決方案可以任何一個幫助: - 我的代碼是:----讀取文件夾中的文件

$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); 

回答

2
<?php 
$dir = '../videos/'; 
if($dh = opendir($dir)){ 
    echo '<select size="4" name="vidfile">'; 
    while(($file=readdir($dh)) !== FALSE){ 
     $file = $dir . '/' . $file; 
     if(!is_file($file) || substr($file, 0, 1) == '.'){ 
     continue; 
     } 
     printf('<option>%s</option>', htmlentities($file)); 
    } 
    echo '</select>'; 
    closedir($dh); 
} 
?> 

首先,如果執行opendir失敗了,不要濫發繼續。它忽略以點開頭的非文件(目錄)和文件(包括。,..,.htaccess等)

1

這是一個與glob()一起使用的變體,它有一個排除文件數組,並收集所有文件option元素,然後將它們作爲一個輸出。另外,​​3210允許通過其大括號語法過濾某些文件擴展名,如未評論的行中所示。

$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; 
0

只是if ($file[0] == ".") continue;而不是這些2如果可能是你在找什麼。
雖然我會略微不同,分2部分。
獲取數據部分:

<?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> 

似乎方式更清潔,我

13

快速和簡單的解決方案

你可以做你的生活更輕鬆,並使用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>'; 

改進:去耦目錄過濾從選擇框大廈

如果要分離從foreach環路濾波邏輯,你也可以繼承一個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')); 

當您在過濾迭代器使用foreach,它會自動觸發accept()。如果accept()返回FALSE,則當前元素將在迭代過濾出。

您創建這樣的選擇框,然後:

echo '<select name="vids" size="4">'; 
foreach($iterator as $file) { 
    printf("<option>%s</option>\n", htmlentities($file->getBasename())); 
} 
echo '</select>'; 

替代繼承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';}); 

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(); 
    } 
} 

,然後打印一個選擇框從一個Iterator很簡單,只要

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> 
+0

爲什麼相同(===)運算符'$文件 - > ISFILE()=== TRUE'?我可以想象,只是'$ file-> isFile()'會起同樣的作用。 – 2012-12-13 22:42:24

+0

對於DirectoryIterator爲+1 – 2013-02-20 13:28:02

相關問題