PHP:从 readdir 到 scandir?

发布于 2024-09-09 02:05:39 字数 609 浏览 7 评论 0原文

我想知道如何将以下代码片段准确地转换为 scandir 而不是 readdir

$path = 'files';

//shuffle files
$count = 0;
if ($handle = opendir($path)) {
    $retval = array();
    while (false !== ($file = readdir($handle))) {
        $ext = pathinfo($file, PATHINFO_EXTENSION);
        if ($file != '.' && $file != '..' && $file != '.DS_Store' &&
          $file != 'Thumbs.db') {
            $retval[$count] = $file;
            $count = $count + 1;
            } else {
            //no proper file
        }
    }
    closedir($handle);
}
shuffle($retval);

i wonder how i can transform exactly the following piece of code to scandir instead of readdir?

$path = 'files';

//shuffle files
$count = 0;
if ($handle = opendir($path)) {
    $retval = array();
    while (false !== ($file = readdir($handle))) {
        $ext = pathinfo($file, PATHINFO_EXTENSION);
        if ($file != '.' && $file != '..' && $file != '.DS_Store' &&
          $file != 'Thumbs.db') {
            $retval[$count] = $file;
            $count = $count + 1;
            } else {
            //no proper file
        }
    }
    closedir($handle);
}
shuffle($retval);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不顾 2024-09-16 02:05:39

scandir 返回,引用

返回文件名数组
成功,或失败则为 FALSE。

这意味着您将获得目录中文件的完整列表,然后可以使用带有 foreach 的自定义循环或某些过滤函数(例如 array_filter

Not tested, but I suppose something like this should to the trick :

$path = 'files';
if (($retval = scandir($path)) !== false) {
    $retval = array_filter($retval, 'filter_files');
    shuffle($retval);
}


function filter_files($file) {
    return ($file != '.' && $file != '..' && $file != '.DS_Store' && $file != 'Thumbs.db');
}

基本上,这里:

  • 首先,使用 scandir 获取文件列表
  • 然后,使用 array_filter 和自定义过滤函数过滤掉不需要的文件
    • 注意:此自定义过滤函数可以使用匿名函数编写,且 PHP >= 5.3
  • 最后,您打乱结果数组。

scandir returns, quoting :

Returns an array of filenames on
success, or FALSE on failure.

Which means you'll get the full list of files in a directory -- and can then filter those, using either a custom-made loop with foreach, or some filtering function like array_filter.

Not tested, but I suppose something like this should to the trick :

$path = 'files';
if (($retval = scandir($path)) !== false) {
    $retval = array_filter($retval, 'filter_files');
    shuffle($retval);
}


function filter_files($file) {
    return ($file != '.' && $file != '..' && $file != '.DS_Store' && $file != 'Thumbs.db');
}

Basically, here :

  • First, you get the list of files, using scandir
  • Then, you filter out the ones you dn't want, using array_filter and a custom filtering function
    • Note : this custom filtering function could have been written using an anonymous function, with PHP >= 5.3
  • And, finally, you shuffle the resulting array.
许一世地老天荒 2024-09-16 02:05:39

不确定为什么要这样做,但这里有一个更简洁的解决方案:

$path = 'files';
$files = array();
foreach (new DirectoryIterator($path) as $fileInfo) {
    if($fileInfo->isDot() || $fileInfo->getFilename() == 'Thumbs.db') continue;
    $files[] = $fileInfo->getFilename();
}
shuffle($files);

Not sure why you want to do that, here's a much more concise solution though:

$path = 'files';
$files = array();
foreach (new DirectoryIterator($path) as $fileInfo) {
    if($fileInfo->isDot() || $fileInfo->getFilename() == 'Thumbs.db') continue;
    $files[] = $fileInfo->getFilename();
}
shuffle($files);
傲影 2024-09-16 02:05:39

要开始解决此类问题,请务必查阅 PHP 手册并阅读注释,这总是非常有帮助的。它指出 scandir 返回一个数组,因此您可以使用 foreach

为了能够删除数组中的某些条目,这里有一个使用 for 的示例:

$exclude = array( ".", "..", ".DS_Store", "Thumbs.db" );
if( ($dir = scandir($path)) !== false ) {
    for( $i=0; $i<count($dir); $i++ ) {
        if( in_array($dir[$i], $exclude) )
            unset( $dir[$i] );
    }
}

$retval = array_values( $dir );

另请参阅 SPL 迭代器 PHP 提供,特别是 RecursiveDirectoryIteratorDirectoryIterator

To get started with such problems always consult the PHP manual and read the comments, it's always very helpful. It states that scandir returns an array, so you can walk through it with foreach.

In order to be able to delete some entries of the array, here's an example with for:

$exclude = array( ".", "..", ".DS_Store", "Thumbs.db" );
if( ($dir = scandir($path)) !== false ) {
    for( $i=0; $i<count($dir); $i++ ) {
        if( in_array($dir[$i], $exclude) )
            unset( $dir[$i] );
    }
}

$retval = array_values( $dir );

Also have a look at the SPL iterators PHP provides, especially RecursiveDirectoryIterator and DirectoryIterator.

臻嫒无言 2024-09-16 02:05:39

这里有一个小功能可以扫描目录而不获取烦人的文件。

function cleanscandir ($dir) {
  $list = [];
  $junk = array('.', '..', 'Thumbs.db', '.DS_Store');
  if (($rawList = scandir($dir)) !== false) {
    foreach (array_diff($rawList, $junk) as $value) {
      $list[] = $value;
    }
    return $list;
  }
  return false;
}

输出一个数组或 false,就像 scandir 一样

Here's a little function to scan a directory without getting the annoying files.

function cleanscandir ($dir) {
  $list = [];
  $junk = array('.', '..', 'Thumbs.db', '.DS_Store');
  if (($rawList = scandir($dir)) !== false) {
    foreach (array_diff($rawList, $junk) as $value) {
      $list[] = $value;
    }
    return $list;
  }
  return false;
}

Outputs an array or false just like scandir does

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文