如何使用PHP检索列表中目录上的文件


How to retrieve files on a directory in list using PHP

如何在某个目录中检索不同的文件扩展名。比方说,我有一个名为"downloads/"的文件夹,文件夹中有不同类型的文件,如PDF、JPEG、DOC文件等。因此,在我的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中做到这一点吗?非常感谢!

这很简单。老实说,我不知道你为什么不在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']);
    }
  }
?>

您可以使用glob&pathinfo:

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

您将尝试以下代码:

$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/>
    ";
}