2012-07-30 21 views
2

我如何能夠在某個目錄中檢索不同的文件擴展名。假設我有一個名爲「downloads /」的文件夾,其中的文件夾是不同類型的文件,如PDF,JPEG,DOC文件等。所以在我的PHP代碼中,我希望檢索這些文件並列出文件名和文件擴展名。例如:Inside「的下載/」文件夾中是不同的文件如何使用PHP在列表中檢索目錄中的文件

downloads/ 

- My Poem.doc 
- My Photographs.jpg 
- My Research.pdf 

,所以我想查看這些文件在那裏我能得到有文件名,文件擴展名和文件目錄。所以鑑於將這樣的事情是這樣的

Title: My Poem 
Type: Document 
Link: [url here] 

Title: My Photographs 
Type: Image 
Link: [url here] 

Title: My Research 
Type: PDF 
Link: [url here] 

任何人都知道如何做到這一點在PHP?非常感謝!

+0

http://whathaveyoutried.com - 在谷歌一次搜索會告訴你該怎麼做。在'readdir()'參見PHP Documnentation http://php.net/manual/en/function.readdir.php – fdomig 2012-07-30 09:53:44

+0

以['opendir()']開頭(http://php.net/manual/en/function .opendir.php)和/或['glob()'](http://php.net/manual/en/function.glob.php) – DaveRandom 2012-07-30 09:53:59

+0

Try [This](http://bit.ly/ObPQY3) – Hameed 2012-07-30 10:02:08

回答

3

這很簡單。說實話,我不知道你爲什麼沒有在php.net上搜索。他們爲此得到了很多例子。檢查它在這裏:click

例子:

<?php 

    function process_dir($dir,$recursive = FALSE) { 
    if (is_dir($dir)) { 
     for ($list = array(),$handle = opendir($dir); (FALSE !== ($file = readdir($handle)));) { 
     if (($file != '.' && $file != '..') && (file_exists($path = $dir.'/'.$file))) { 
      if (is_dir($path) && ($recursive)) { 
      $list = array_merge($list, process_dir($path, TRUE)); 
      } else { 
      $entry = array('filename' => $file, 'dirpath' => $dir); 

//---------------------------------------------------------// 
//      - SECTION 1 -      // 
//   Actions to be performed on ALL ITEMS   // 
//----------------- Begin Editable ------------------// 

    $entry['modtime'] = filemtime($path); 

//-----------------  End Editable  ------------------// 
      do if (!is_dir($path)) { 
//---------------------------------------------------------// 
//      - SECTION 2 -      // 
//   Actions to be performed on FILES ONLY   // 
//----------------- Begin Editable ------------------// 

    $entry['size'] = filesize($path); 
    if (strstr(pathinfo($path,PATHINFO_BASENAME),'log')) { 
    if (!$entry['handle'] = fopen($path,r)) $entry['handle'] = "FAIL"; 
    } 

//-----------------  End Editable  ------------------// 
       break; 
      } else { 
//---------------------------------------------------------// 
//      - SECTION 3 -      // 
//  Actions to be performed on DIRECTORIES ONLY  // 
//----------------- Begin Editable ------------------// 

//-----------------  End Editable  ------------------// 
       break; 
      } while (FALSE); 
      $list[] = $entry; 
      } 
     } 
     } 
     closedir($handle); 
     return $list; 
    } else return FALSE; 
    } 

    $result = process_dir('C:/webserver/Apache2/httpdocs/processdir',TRUE); 

// Output each opened file and then close 
    foreach ($result as $file) { 
    if (is_resource($file['handle'])) { 
     echo "\n\nFILE (" . $file['dirpath'].'/'.$file['filename'] . "):\n\n" . fread($file['handle'], filesize($file['dirpath'].'/'.$file['filename'])); 
     fclose($file['handle']); 
    } 
    } 

?> 
+0

對於'do ... if ... else ... while(FALSE)'+1。 – salathe 2012-07-31 06:44:52

0

你可以使用glob & pathinfo

<?php 
foreach (glob(__DIR__.'/*') as $filename) { 
    print_r(pathinfo($filename)); 
} 
?> 
0

你試試這個代碼:

$Name = array(); 
$ext = array(); 
$downloadlink = array(); 
$dir = 'downloads' 
while ($file= readdir($dir)) 
{ 

    if ($file!= "." && $file!= "..") 
    { 
     $temp = explode(".",$file); 
     if (count($temp) > 1) 
      { 
       $Name[count($Name)] = $temp[0]; 
       swich($ext) 
       { 
        case "doc" : 
        { 
         $ext[count($ext)] = "Document"; 
         break; 
        } 
        [...] 
        default : 
        { 
         $ext[count($ext)] = "Error"; 
         break; 
        } 
       } 
       $downloadlink[count($downloadlink)] = "http://yourdomain.com/".$dir."/".$file; 
      } 
    } 
} 
closedir($dir); 

for($aux = 0; $aux < count($Name); $aux++) 
{ 
    echo "Name = " . $Name[$aux]."<br /> 
    Type = " . $ext[$aux]."<br/> 
    Link = " . $downloadlink[$aux]."<br/> 
    "; 
} 
相關問題