读取一个文件名“;仅“;然后跳到下一个目录-PHP


Read one file name "Only" and skip to next directory - PHP

我有以下代码,它从每个目录读取所有文件名,但我希望它"只"读取一个文件名,然后跳到下一个目录。

<?php
$dir = "/images/";
// Open a directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
      echo "filename:" . $file . "<br>";
    }
    closedir($dh);
  }
}
?>

如何只读取一个文件名,然后跳到下一个目录只读取"第一个"文件名,以此类推。如果您需要更多信息,请告诉我。

如果读取哪个文件无关紧要,那么只需在while循环中放入break;,或者甚至不进入循环,只需获取$file = readdir($dh);PS:在linux操作系统中要注意...。还要查找scabdir()函数

您需要在此处维护counter才能执行同样的操作。

这样做:

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

让我知道更多的帮助!