I need to get 15 random images from a folder and show them on a page: I tried the following code, however it did not do what I wanted:
我需要从一个文件夹中获取15个随机图像并在页面上显示它们:我尝试了以下代码,但它没有按照我的意愿执行:
$string =array();
$filePath='wp-content/themes/tema/img-test/';
$dir = opendir($filePath);
while ($file = readdir($dir)) {
if (eregi("\.png",$file) || eregi("\.jpg",$file) || eregi("\.gif",$file) ) {
$string[] = $file;
}
}
while (sizeof($string) != 0){
$img = array_pop($string);
echo "<img src='$filePath$img' width='100px'/>";
}
5 个解决方案
#1
1
So, you have all the files in $string
array, that's good.
所以,你有$ string数组中的所有文件,这很好。
You can either use the rand()
function to get some random integer in the arrays size:
你可以使用rand()函数来获取数组大小的随机整数:
$string = ['img1.jpg','img2.jpg','img3.jpg'];
$rand = rand(0,count($string)-1);
echo $string[$rand];
You would have to loop that.
你必须循环它。
Or, you could use array_rand()
which will automate all that:
或者,您可以使用array_rand()来自动化所有这些:
$string = ['img1.jpg','img2.jpg','img3.jpg'];
$amount = 3;
$rand_arr = array_rand($string, $amount);
for($i=0;$i<$amount;$i++) {
echo $string[$rand_arr[$i]] ."<br>";
}
#2
0
You could do this using the glob()
function native to PHP. It will get all files in a directory. Following that you can pick one file from the retrieved list.
您可以使用PHP原生的glob()函数来完成此操作。它将获取目录中的所有文件。之后,您可以从检索到的列表中选择一个文件。
$randomFiles = array();
$files = glob($dir . '/*.*');
$file = array_rand($files);
for ($i = 0; $i <= 15; $i++) {
$randomFiles[] = $files[$file];
}
#3
0
Use this code. Your random image will be available in $arRandomFiles.
使用此代码。您的随机图片将在$ arRandomFiles中提供。
$filePath = 'wp-content/themes/tema/img-test/';
$files = glob($filePath. '*.{jpeg,gif,png}', GLOB_BRACE);
$arKeys = array_rand($files, 15);
$arRandomFiles = array();
foreach ($arKeys as $key) {
$arRandomFiles[] = $files[$key];
}
var_dump($arRandomFiles);
#4
0
Simple function that handles that
处理它的简单功能
<?php
function getImg( $path ) {
$filePath= $path . '*';
$imgs = glob( $filePath );
if( $imgs ) {
$i = 1;
foreach( $imgs as $img ) {
if( $i <= 15 ) {
$ext = pathinfo( $img, PATHINFO_EXTENSION );
if( in_array( $ext, array( 'jpg', 'jpeg', 'png', 'gif' ) ) )
$r[] = $img;
}
else
break;
$i++;
}
shuffle( $r );
return $r;
}
else
return array();
}
print_r( getImg( 'wp-content/themes/tema/img-test/' ) );
#5
0
You can try function like:
您可以尝试以下功能:
function getRandomFile($directory)
{
$directoryIterator = new DirectoryIterator($directory);
$count = iterator_count($directoryIterator) - 2;
foreach ($directoryIterator as $fileInfo) {
$last = $fileInfo->getRealPath();
if ($fileInfo->isFile() && (rand() % $count == 0)) {
break;
}
}
return $last;
}
#1
1
So, you have all the files in $string
array, that's good.
所以,你有$ string数组中的所有文件,这很好。
You can either use the rand()
function to get some random integer in the arrays size:
你可以使用rand()函数来获取数组大小的随机整数:
$string = ['img1.jpg','img2.jpg','img3.jpg'];
$rand = rand(0,count($string)-1);
echo $string[$rand];
You would have to loop that.
你必须循环它。
Or, you could use array_rand()
which will automate all that:
或者,您可以使用array_rand()来自动化所有这些:
$string = ['img1.jpg','img2.jpg','img3.jpg'];
$amount = 3;
$rand_arr = array_rand($string, $amount);
for($i=0;$i<$amount;$i++) {
echo $string[$rand_arr[$i]] ."<br>";
}
#2
0
You could do this using the glob()
function native to PHP. It will get all files in a directory. Following that you can pick one file from the retrieved list.
您可以使用PHP原生的glob()函数来完成此操作。它将获取目录中的所有文件。之后,您可以从检索到的列表中选择一个文件。
$randomFiles = array();
$files = glob($dir . '/*.*');
$file = array_rand($files);
for ($i = 0; $i <= 15; $i++) {
$randomFiles[] = $files[$file];
}
#3
0
Use this code. Your random image will be available in $arRandomFiles.
使用此代码。您的随机图片将在$ arRandomFiles中提供。
$filePath = 'wp-content/themes/tema/img-test/';
$files = glob($filePath. '*.{jpeg,gif,png}', GLOB_BRACE);
$arKeys = array_rand($files, 15);
$arRandomFiles = array();
foreach ($arKeys as $key) {
$arRandomFiles[] = $files[$key];
}
var_dump($arRandomFiles);
#4
0
Simple function that handles that
处理它的简单功能
<?php
function getImg( $path ) {
$filePath= $path . '*';
$imgs = glob( $filePath );
if( $imgs ) {
$i = 1;
foreach( $imgs as $img ) {
if( $i <= 15 ) {
$ext = pathinfo( $img, PATHINFO_EXTENSION );
if( in_array( $ext, array( 'jpg', 'jpeg', 'png', 'gif' ) ) )
$r[] = $img;
}
else
break;
$i++;
}
shuffle( $r );
return $r;
}
else
return array();
}
print_r( getImg( 'wp-content/themes/tema/img-test/' ) );
#5
0
You can try function like:
您可以尝试以下功能:
function getRandomFile($directory)
{
$directoryIterator = new DirectoryIterator($directory);
$count = iterator_count($directoryIterator) - 2;
foreach ($directoryIterator as $fileInfo) {
$last = $fileInfo->getRealPath();
if ($fileInfo->isFile() && (rand() % $count == 0)) {
break;
}
}
return $last;
}