Php 使用OPENDIR()选择随机文件

Php 使用OPENDIR()选择随机文件,php,opendir,Php,Opendir,我试过: function random_pic($dir = '../myfolder') { $files = opendir($dir . '/*.*'); $file = array_rand($files); return $files[$file]; } 此函数使用glob工作,但不使用opendir 这将返回打开目录失败错误。我猜opendir不能接受像**?是否可以选择文件夹中的所有文件并随机选择一个?opendir函数不会返回文件/文件夹列表。它将只打

我试过:

function random_pic($dir = '../myfolder') {
    $files = opendir($dir . '/*.*');
    $file = array_rand($files);
    return $files[$file];
}
此函数使用glob工作,但不使用opendir

这将返回打开目录失败错误。我猜opendir不能接受像**?是否可以选择文件夹中的所有文件并随机选择一个?

opendir函数不会返回文件/文件夹列表。它将只打开closedir、readdir或rewinddir可以使用的句柄。这里正确的用法应该是glob,但我看到您不希望这样,您也可以使用scandir,如下所示:

<?php
$path = "./";

$files = scandir($path);
shuffle($files);

for($i = 0; ($i < count($files)) && (!is_file($files[$i])); $i++);

echo $files[$i];
?>
在你承认我没有错后,我很乐意选择时机,看看这是否需要更长的时间,或者glob是否需要更长的时间

以下两种方法使用opendir快速读取目录并返回随机文件或目录

所有的基准测试都使用CI3,平均为100个脉冲。在Win10 Intel i54460 w/16GB RAM上使用WAMP 获取随机文件: 使用简单到:
//  Benchmark 0.0018 seconds *
$this->getRandomFile('directoryName');
//  would pull random contents of file from given directory

//  Benchmark 0.0017 seconds *
$this->getRandomFile('directoryName', 'php');
//  |OR|
$this->getRandomFile('directoryName', ['php', 'htm']);
//  one gets a random php file 
//  OR gets random php OR htm file contents

//  Benchmark 0.0018 seconds *
$this->getRandomFile('directoryName', NULL, FALSE);
//  returns random file name

//  Benchmark 0.0019 seconds *
$this->getRandomFile('directoryName', NULL, 'path');
//  returns random full file path
//  Benchmark 0.0013 seconds *
$this->getRandomDir('parentDirectoryName');
//  returns random full directory path of dirs found in given directory

//  Benchmark 0.0015 seconds *
$this->getRandomDir('parentDirectoryName', FALSE);
//  returns random directory name

//  Benchmark 0.0015 seconds *
$this->getRandomDir('parentDirectoryName', FALSE, 'dirNameContains');
//  returns random directory name
获取随机目录: 使用简单到:
//  Benchmark 0.0018 seconds *
$this->getRandomFile('directoryName');
//  would pull random contents of file from given directory

//  Benchmark 0.0017 seconds *
$this->getRandomFile('directoryName', 'php');
//  |OR|
$this->getRandomFile('directoryName', ['php', 'htm']);
//  one gets a random php file 
//  OR gets random php OR htm file contents

//  Benchmark 0.0018 seconds *
$this->getRandomFile('directoryName', NULL, FALSE);
//  returns random file name

//  Benchmark 0.0019 seconds *
$this->getRandomFile('directoryName', NULL, 'path');
//  returns random full file path
//  Benchmark 0.0013 seconds *
$this->getRandomDir('parentDirectoryName');
//  returns random full directory path of dirs found in given directory

//  Benchmark 0.0015 seconds *
$this->getRandomDir('parentDirectoryName', FALSE);
//  returns random directory name

//  Benchmark 0.0015 seconds *
$this->getRandomDir('parentDirectoryName', FALSE, 'dirNameContains');
//  returns random directory name
在组合中使用,如: 只是一个关于glob&scandir的注释。我使用每种方法制作了getRandomDir的替代版本。使用scandir时,从-0.001到+0.003之间的基准测试差异非常小,但使用glob时的速度明显较慢!每次通话的差异在+5到+1.100之间


和使用glob有什么问题?另外,为了使用…dir函数,在这种情况下需要使用scandir函数,好吗?除非你用它做了错误的事情……opendir在你的代码中毫无意义。它根据相关的手动输入返回一个资源。你在打电话给array_rand。显然,这是行不通的,因为资源不是数组。不知怎的,我怀疑你在同一个任务中对opendir和glob之间的差异进行了基准测试,因为这就像把苹果比作天花一样。他们不一样。投票赞成苹果和天花的比较。opendir可能不会更快,但它可能会稍微提高内存效率,因为您不必生成整个数组。我很高兴您能让我承认我是否认为您是对的,基准测试结果显示您是错的。谢谢你的意见。什么意思基准测试结果显示我错了。。。这毫无意义。这里不能使用opendir。您可以选择scandir和glob。在我继续讨论之前,这就是我所知道的我们正在争论的问题。你认为opendir在这里有用吗?另一种选择是readdir,它将使用opendir创建的句柄,但opendir仍然不返回文件数组…我的第一语言是英语,如果不是英语,那就不重要了。。。。我的问题是,你所说的与我试图向你解释的内容无关。你也忽视了我的要求,你能解释一下吗?@Norse:你猜怎么着?更快地搞砸仍然只是搞砸了。除非两个程序都能正常工作,否则性能比较毫无意义。因为你的显然不是,任何比较都是毫无意义的。无论做错事需要多长时间,我都投了反对票,因为我觉得这并没有回答问题,当我有同样的问题时,这对我也没有帮助。我真的不在乎你和op之间的争吵,我只是想要最好的答案。今天我花了几个小时在这些方法上,只是为了测试什么最有效,我修改了我的答案,试图展示更多的结果。最后,我喜欢opendir,正如前面提到的,它与scandir几乎没有区别。祝你周末愉快…如果你愿意的话?为什么?同样,这个答案是有欺骗性的,因为opendir并不是真正选择随机变量的函数file@Jeremy没有什么欺骗性的。它按照OP的要求做,也按照我的要求做。它确实使用opendir,正如opendir所希望的那样。你的答案根本不正确。opendir意味着打开一个目录进行读取,然后使用readdir获取所需信息,然后关闭并继续。它非常简单,基准测试也非常好。很抱歉,您以前的回答没有对这个问题得出正确的结论,在寻找这个答案时,肯定没有帮助我。
$dir = $this->getRandomDir('dirName');
$file = $this->getRandomFile($dir, 'mp3', FALSE);
//  returns a random mp3 file name. 
//  Could be used to load random song via ajax.
/** getRandomFile(String)
 *  Simple method for retrieving a random file from a directory
 **/
function getRandomFile($path, $type=NULL, $contents=TRUE) { if (strpos($path, $_SERVER['DOCUMENT_ROOT']) === FALSE) $path = $_SERVER['DOCUMENT_ROOT'] . '/' . $path; if (is_dir($path)) { if ($dh = opendir($path)) { $arr = []; while (false !== ($file = readdir($dh))) { if (!is_dir("$path/$file") && !preg_match('/^\.{1,2}$/', $file)) { if(is_null($type)) $arr[] = $file; elseif (is_string($type) && preg_match("/\.($type)$/", $file)) $arr[] = $file; elseif (is_array($type)) { $type = implode('|', $type); if (preg_match("/\.($type)$/", $file)) $arr[] = $file; } } } closedir($dh); if (!empty($arr)) { shuffle($arr); $file = $arr[mt_rand(0, count($arr)-1)]; return empty($contents) ? $file : ($contents == 'path' ? "$path/$file" : file_get_contents($file)); } } } return NULL; }

/** getRandomDir(String)
 *  Simple method for retrieving a random directory
 **/
function getRandomDir($path, $full=TRUE, $indexOf=NULL) { if (strpos($path, $_SERVER['DOCUMENT_ROOT']) === FALSE) $path = $_SERVER['DOCUMENT_ROOT'] . '/' . $path; if (is_dir($path)) { if ($dh = opendir($path)) { $arr = []; while (false !== ($dir = readdir($dh))) { if (is_dir("$path/$dir") && !preg_match('/^\.{1,2}$/', $dir)) { if(is_null($indexOf)) $arr[] = $file; if (is_string($indexOf) && strpos($dir, $indexOf) !== FALSE) $arr[] = $dir; elseif (is_array($indexOf)) { $indexOf = implode('|', $indexOf); if (preg_match("/$indexOf/", $dir)) $arr[] = $dir; } } } closedir($dh); if (!empty($arr)) { shuffle($arr); $dir = $arr[mt_rand(0, count($arr)-1)]; return $full ? "$path/$dir" : $dir; } } } return NULL; }

/*  This is only here to make copying easier.   */