2012-08-24 40 views
2

我想弄清楚如何在CodeIgniter中使用這個directory_map函數。 詳情請參見這裏的手冊:http://codeigniter.com/user_guide/helpers/directory_helper.html使用CodeIgniter目錄映射助手

這裏是我的工作(種),結果波紋管:

$this->load->helper('directory'); 
$map = directory_map('textfiles/'); 

$index = ''; 

foreach ($map as $dir => $file) { 
    $idx .= "<p> dir: {$dir} </p> <p> file: {$file} </p>"; 
} #foreach 

return $idx; 

我的測試環境中的目錄和文件結構:

one [directory] 
    subone [sub-directory] 
    testsubone.txt [file-in-sub-directory] 
    testone.txt [file-in-directory-one] 
three [directory] 
    testthree.txt [file-in-directory-three] 
two [directory] 
    testing [sub-directory] 
    testagain.txt [file-in-sub-directory-testing] 
    test.txt [file-in-directory-testing] 
test.txt [file] 

這是輸出結果,我有我的觀點:

dir: 0 
dir: two 
file: Array 
dir: three 
file: Array 
dir: 1 
file: test.txt 
dir: one 
file: Array 

正如你在這個結果中看到的,並不是所有的目錄或文件都列出來了,有些顯示爲一個數組。

在文件助手中還有一些名爲「get_filenames」的函數。也許它可以以某種方式用於directory_map。

而且,我得到這個以下錯誤:

A PHP Error was encountered 
Severity: Notice 
Message: Array to string conversion 
Filename: welcome.php 
Line Number: # 

任何幫助將大大appreceated。謝謝=)

回答

2

你的問題是,你試圖打印出一個多維的數組。

你應該嘗試這樣做,而不是:
隨着深度計http://codepad.org/y2qE59XS

$map = directory_map("./textfiles/"); 

function print_dir($in,$depth) 
{ 
    foreach ($in as $k => $v) 
    { 
     if (!is_array($v)) 
      echo "<p>",str_repeat("&nbsp;&nbsp;&nbsp;",$depth)," ",$v," [file]</p>"; 
     else 
      echo "<p>",str_repeat("&nbsp;&nbsp;&nbsp;",$depth)," <b>",$k,"</b> [directory]</p>",print_dir($v,$depth+1); 
    } 
} 

print_dir($map,0); 

編輯,另一個版本沒有深度計:http://codepad.org/SScJqePV

function print_dir($in) 
{ 
    foreach ($in as $k => $v) 
    { 
     if (!is_array($v)) 
      echo "[file]: ",$v,"\n"; 
     else 
      echo "[directory]: ",$k,"\n",print_dir($v); 
    } 
} 

print_dir($map); 

請,更具體和你想要的輸出。

編輯簡評
這一個保持軌跡跟蹤http://codepad.org/AYDIfLqW

function print_dir($in,$path) 
{ 
    foreach ($in as $k => $v) 
    { 
     if (!is_array($v)) 
      echo "[file]: ",$path,$v,"\n"; 
     else 
      echo "[directory]: ",$path,$k,"\n",print_dir($v,$path.$k.DIRECTORY_SEPARATOR); 
    } 
} 

print_dir($map,''); 

最後編輯
返回功能http://codepad.org/PEG0yuCr

function print_dir($in,$path) 
{ 
    $buff = ''; 
    foreach ($in as $k => $v) 
    { 
     if (!is_array($v)) 
      $buff .= "[file]: ".$path.$v."\n"; 
     else 
      $buff .= "[directory]: ".$path.$k."\n".print_dir($v,$path.$k.DIRECTORY_SEPARATOR); 
    } 
    return $buff; 
} 
+0

http://codepad.org/y2qE59XS這與輸出你的輸入數組...我沒有得到你想要打印出來的東西?!你能舉一個你想要什麼輸出的例子嗎? – Touki

+0

只需使用'$ map = directory_map(「./textfiles /」)'而不是? – Touki

+0

是的我知道,我用$ this-> load-> helper('directory')使用了你的「print_dir」函數; $ map = directory_map('./ textfiles /'); $這 - > print_dir($地圖);但所有我得到的輸出只有一個目錄和一個文件。 [file]:test.txt [directory]:兩個 – Tux