Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP检索列表中目录上的文件_Php - Fatal编程技术网

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

如何使用PHP检索列表中目录上的文件,php,Php,如何在某个目录中检索不同的文件扩展名。假设我有一个名为“downloads/”的文件夹,其中包含不同类型的文件,如PDF、JPEG、DOC文件等。因此,在我的PHP代码中,我希望检索这些文件并列出它们的文件名和文件扩展名。示例:“downloads/”文件夹中有不同的文件 downloads/ - My Poem.doc - My Photographs.jpg - My Research.pdf 所以我想查看那些文件,在那里我可以得到文件名、文件扩展名和文件目录。所以在视图中会是这样

如何在某个目录中检索不同的文件扩展名。假设我有一个名为“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实现吗?非常感谢

您可以使用&:


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

?>

您将尝试以下代码:

$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/>
    ";
}
$Name=array();
$ext=array();
$downloadlink=array();
$dir='downloads'
而($file=readdir($dir))
{
如果($file!=“&&&$file!=”)
{
$temp=explode(“.”,$file);
如果(计数($temp)>1)
{
$Name[计数($Name)]=$temp[0];
swich($ext)
{
案例“doc”:
{
$ext[计数($ext)]=“文件”;
打破
}
[...]
违约:
{
$ext[计数($ext)]=“错误”;
打破
}
}
$downloadlink[计数($downloadlink)]=”http://yourdomain.com/“$dir.”/“$file;
}
}
}
closedir($dir);
对于($aux=0;$aux
Type=“.$ext[$aux]”
Link=“.$downloadlink[$aux]”,
"; }
从开始和/或尝试
$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/>
    ";
}