代码之家  ›  专栏  ›  技术社区  ›  Arun Annamalai

如何使用PHP从文件夹中读取文件列表?

  •  37
  • Arun Annamalai  · 技术社区  · 17 年前

    有什么简单的脚本可以实现吗?

    10 回复  |  直到 17 年前
        1
  •  114
  •   xkeshav    14 年前

    最简单和最有趣的方式(imo)是 glob

    foreach (glob("*.*") as $filename) {
        echo $filename."<br />";
    }
    

    但标准的方法是使用 directory functions.

    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) {
                echo "filename: .".$file."<br />";
            }
            closedir($dh);
        }
    }
    

    还有 SPL DirectoryIterator methods . 如果你感兴趣

        2
  •  16
  •   bar    11 年前

    有这个功能 斯堪的纳维亚

    $dir = 'dir';
    $files = scandir($dir, 0);
    for($i = 2; $i < count($files); $i++)
        print $files[$i]."<br>";
    

    More here in the php.net manual

        3
  •  16
  •   Sebastian Viereck    7 年前

    这就是我喜欢做的:

    $files = array_values(array_filter(scandir($path), function($file) use ($path) { 
        return !is_dir($path . '/' . $file);
    }));
    
    foreach($files as $file){
        echo $file;
    }
    
        4
  •  3
  •   MontDeska    13 年前

    如果您在访问路径时遇到问题,可能需要:

    $root = $_SERVER['DOCUMENT_ROOT'];
    $path = "/cv/"; 
    
    // Open the folder
     $dir_handle = @opendir($root . $path) or die("Unable to open $path");
    
        6
  •  2
  •   Rishabh    11 年前

    签入多个文件夹:

    <?php
    $arr = array("folder_1","folder_2");
    $format  = ".csv";
    
    for($x=0;$x<count($arr);$x++){
        $mm = $arr[$x];
    
        foreach (glob("$mm/*$format") as $filename) {
            echo "$filename size " . filesize($filename) . "<br>";
        }
    }
    ?>
    
        7
  •  2
  •   Vlad Miller    10 年前

    您可以使用标准目录函数

    $dir = opendir('/tmp');
    while ($file = readdir($dir)) {
        if ($file == '.' || $file == '..') {
            continue;
        }
    
        echo $file;
    }
    closedir($dir);
    
        8
  •  0
  •   Community Mohan Dere    9 年前

    还有一个非常简单的方法可以在 RecursiveTreeIterator 同学们,回答如下: https://stackoverflow.com/a/37548504/2032235

        9
  •  0
  •   alex james    9 年前
    <html>
    <head>
    <title>Names</title>
    </head>
    <body style="background-color:powderblue;">
    
    <form method='post' action='alex.php'>
     <input type='text' name='name'>
    <input type='submit' value='name'>
    </form>
    Enter Name:
    <?php
    
      if($_POST)
      {
      $Name = $_POST['name'];
      $count = 0;
      $fh=fopen("alex.txt",'a+') or die("failed to create");
      while(!feof($fh))
      {
        $line = chop(fgets($fh));
        if($line==$Name && $line!="")
        $count=1;
      }
      if($count==0 && $Name!="")
      {
        fwrite($fh, "\r\n$Name"); 
     }
      else if($count!=0 && $line!="") 
      { 
        echo '<font color="red">'.$Name.', the name you entered is already in the list.</font><br><br>';
      }
      $count=0;
      fseek($fh, 0);
      while(!feof($fh))
      {
        $a = chop(fgets($fh));
        echo $a.'<br>';
        $count++;
      }
      if($count<=1)
      echo '<br>There are no names in the list<br>';
      fclose($fh);
      }
    ?>
    </body>
    </html>
    
    推荐文章