[英]Randomly select a file from directory in PHP
So I have seen solutions of this, however my question is slightly different. 因此,我已经看到了解决方案,但是我的问题略有不同。
I want the file to have a character at the end. 我希望文件末尾有一个字符。
So, for example, there is a directory called imgs: 因此,例如,存在一个名为imgs的目录:
imgs contents: div.png, div2.png, divb.png, divab.png imgs内容:div.png,div2.png,divb.png,divab.png
I need to randomly select a file from this folder, but I need it to have ab at the end. 我需要从该文件夹中随机选择一个文件,但是我需要在文件末尾加上ab。 So I could only get on either divb.png or divab.png. 所以我只能上divb.png或divab.png。
If I get one that doesn't end in b, I need to reselect. 如果得到不以b结尾的数字,则需要重新选择。 I currently have some code that gives me a timeout and doesn't reselect. 我目前有一些代码可以给我超时,并且不会重新选择。
function random_pic($dir = 'imgs'){
$files = glob($dir . '/*.png');
$file = array_rand($files);
if(substr($files[$file], -5)==$shortparam.".png"){
return $files[$file];
} else {
return null;
}
}
EDIT ----------------- 编辑-----------------
<?php
function random_pic() {
$files = glob('imgs/*.png' );
do {
if ( isset( $file ) ) {
unset( $files[$file] );
}
$file = array_rand( $files );
} while ( ( substr( $files[ $file ], -5 != ( $shortparam . ".png" ) ) ) AND ( count( $files) > 0 ) );
if ( count( $files ) > 0 ) {
return $files[ $file ];
} else {
echo $file;
return false;
}
}
for ($i = 0 ; $i < 20; $k++){
$image = random_pic();
if($image == false){
} else {
// display image
This times out for some reason. 由于某种原因,此超时。 (Fatal error: Maximum execution time of 10 seconds exceeded in file.php on line 84) (致命错误:第84行在file.php中超过了10秒的最大执行时间)
Thanks for any help you can give! 谢谢你提供的所有帮助!
I obviously don't have your files and your directory structure to try this code against, but I'm pretty confident it will solve your problem. 我显然没有尝试使用此代码的文件和目录结构,但是我非常有信心它将解决您的问题。
function random_pic( $dir = 'imgs' ) {
if ( $files = glob( $dir . '/*.png' ) ) {
do {
if ( isset( $file ) ) {
unset( $files[$file] );
}
if ( count( $files ) > 0 ) {
$file = array_rand( $files );
}
} while ( ( substr( $files[ $file ], -5 != ( $shortparam . ".png" ) ) ) AND ( count( $files ) > 0 ) );
if ( count( $files ) > 0 ) {
return $files[ $file ];
} else {
return NULL;
}
} else {
return NULL;
}
}
You may want to consider returning FALSE instead of NULL if nothing is found since it's more versatile on the parent end. 如果未找到任何内容,则可能要考虑返回FALSE而不是NULL,因为它在父端更加通用。
You could achieve that with a blend of glob
, array_walk()
, array_rand()
and preg_match()
. 您可以通过混合glob
, array_walk()
, array_rand()
和preg_match()
。
<?php
function random_pic($dir='imgs', $extension=".png", $endChar="b"){
$files = glob($dir . "/*{$extension}");
$matches = array();
array_walk($files, function($imgFile, $index) use ($extension, $endChar, &$matches) {
$pixName = preg_replace("#" . preg_quote($extension) . "#", "", basename($imgFile));
if( preg_match("#" . preg_quote($endChar) . "$#", $pixName)){
$matches[] = $imgFile;
}
});
return (count($matches))? $matches[array_rand($matches)] : null;
}
$randomPic = random_pic(__DIR__. "/imgs", ".png", "b");
// OR JUST USE THE DEFAULTS SINCE THEY ARE JUST THE SAME IN YOUR CASE:
// $randomPic = random_pic();
var_dump($randomPic);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.