2011-09-04 38 views
1

我試圖創建一個目錄中的文件列表,包括子目錄中的文件,顯示其完整路徑,文件名和擴展名。任何指導將不勝感激。如何查看數組中的值?

大量閱讀後,我嘗試了幾件事情,這是最近我發現並嘗試過,但沒有任何喜悅,現在我只是尋找瀏覽器來顯示上述信息。

$dir = '../test/images/'; 
function getFileList($dir, $recurse=false) 
{ 
    // array to hold return value 
    $retval = array(); 
    // add trailing slash if missing 
    if(substr($dir, -1) != "/") $dir .= "/"; 
    // open pointer to directory and read list of files 
    $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading"); 
    while(false !== ($entry = $d->read())) { 
     // skip hidden files 
     if($entry[0] == ".") continue; 
     if(is_dir("$dir$entry")) { 
      $retval[] = array(
       "name" => "$dir$entry/", 
       "type" => filetype("$dir$entry"), 
       "size" => 0, 
       "lastmod" => filemtime("$dir$entry") 
      ); 
      if($recurse && is_readable("$dir$entry/")) { 
       $retval = array_merge($retval, getFileList("$dir$entry/", true)); 
      } 
      } elseif(is_readable("$dir$entry")) { 
      $retval[] = array(
       "name" => "$dir$entry", 
       "type" => mime_content_type("$dir$entry"), 
       "size" => filesize("$dir$entry"), 
       "lastmod" => filemtime("$dir$entry") 
      ); 
     } 
    } 
    $d->close(); 
    return $retval; 
} 
print_r($retval); 

我的最終目標是創建一些PHP上執行將查找的目錄,創建子目錄(有任何子目錄中)具有相同名稱在新目錄中(所以/測試/相冊/會被創建爲/ live/album /)。然後調整每個原始目錄中包含的任何圖像的大小,並將其保存到其相關的新目錄中。我很欣賞這是很多的,並且毫無疑問在我繼續時會有很多問題,但會在以後保存這些問題。

回答

3

這裏有一個類似的腳本,我寫我的網站:

<?php 
$baseDir = '/images/test'; 
$newDir = '/images/live/'; 
$returnFiles = array(); 

function getDirectory($path, $level, $newDir, $baseDir, &$returnFiles) { 
    if(substr($path, -1) != DIRECTORY_SEPARATOR) { 
     $path = $path . DIRECTORY_SEPARATOR; 
    } 
    if(substr($newDir, -1) != DIRECTORY_SEPARATOR) { 
     $newDir = $newDir . DIRECTORY_SEPARATOR; 
    } 
    if(substr($baseDir, -1) != DIRECTORY_SEPARATOR) { 
     $baseDir = $baseDir . DIRECTORY_SEPARATOR; 
    } 
    $dh = opendir($path); 
    if(!$dh) { 
     return; 
    } 
    while(false !== ($file = readdir($dh))) { 
     if($file == '.' || $file == '..') { 
      continue; 
     } 
     $ident = str_repeat("\t", $level); 

     if(is_file($path . $file)) { 
      if(substr(mime_content_type($path . $file), 0, 5) == 'image') { 
       $returnFiles[] = saveImage($path . $file, $newDir, $baseDir); 
      } 
     } 
     else if(is_dir($path . $file)) { 
      getDirectory($path . $file . DIRECTORY_SEPARATOR, $level+1, $newDir, $baseDir, $returnFiles); 
     } 
    } 
    closedir($dh); 
} 

function saveImage($image, $newDir, $baseDir) { 
    $current = dirname($image) . DIRECTORY_SEPARATOR; 
    $path = str_replace($baseDir, $newDir, $current); 
    if(!is_dir($path)) { 
     mkdir($path, 0777, TRUE); 
    } 
    $oldImage = array(
     "name" => $image, 
     "type" => mime_content_type($image), 
     "size" => filesize($image), 
     "lastmod" => filemtime($image) 
    ); 
    // conver image 
    convertImage($image, $path);  
    $image = $path . basename($image); 
    $newImage = array(
     "name" => $image, 
     "type" => mime_content_type($image), 
     "size" => filesize($image), 
     "lastmod" => filemtime($image) 
    ); 
    return array('old' => $oldImage, 'new' => $newImage); 
} 

function convertImage($image, $folder) { 
    // do what you need to do to convert/resize etc.. 
    // for demo purpose I simply copy the image. 
    copy($image, $folder . basename($image)); 
} 

getDirectory($baseDir, 0, $newDir, $baseDir, $returnFiles); 
print_r($returnFiles); 
+0

幹得漂亮! +1是爲了進入OP的問題的根源。 :) – Herbert

+0

由於不太熟悉PHP,我需要花一些時間來解決這個問題,但是想要感謝你對這個問題的廣泛建議。 – dajoto

+0

我得到一個錯誤'致命的錯誤:調用未定義函數mime_content_type()'任何想法,爲什麼這可能是? – dajoto

1

那麼回答你的原始問題,我發現使用var_dump($ array)確實有助於說明傳遞的數組結構,尤其是多維的。 PUS它給你的數據類型,這可能會有所幫助,當你進步。

3

簡單快速:

echo '<pre>'; 
print_r($array); // where $array == array(); 
echo '</pre>'; 
1

下面是一些代碼,我在Ubuntu Forums回升,顯示在一個給定的路徑中的所有目錄及其子目錄。

function recurse($path) 
{ 
    foreach(glob($path.'/*',GLOB_ONLYDIR) as $myDirectory) 
    { 
     // change this to whatever you need to do 
     echo "Directory : $myDirectory<br>\n"; 

     // but keep this line 
     recurse($myDirectory); 
    } 
} 

這應該讓你開始在你的’重新嘗試做什麼。 儘管您可能想要更改函數的名稱,但爲了清晰起見,