2

我如何能够在某个目录中检索不同的文件扩展名。假设我有一个名为“downloads/”的文件夹,该文件夹内有不同类型的文件,如 PDF、JPEG、DOC 文件等。所以在我的 PHP 代码中,我希望检索这些文件并列出文件名和文件扩展名. 示例:在“downloads/”文件夹中是不同的文件

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中做到这一点?非常感谢!

4

3 回答 3

3

这很简单。老实说,我不知道您为什么不在 php.net 上进行搜索。他们为此提供了很多例子。在这里查看:点击

例子:

<?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']);
    }
  }

?>
于 2012-07-30T09:56:30.273 回答
0

你可以使用glob& pathinfo

<?php
foreach (glob(__DIR__.'/*') as $filename) {
    print_r(pathinfo($filename));
}
?>
于 2012-07-30T09:55:17.810 回答
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/>
    ";
}
于 2012-07-30T10:03:42.423 回答