php目录迭代器,洗牌图像 洗牌(图像);

php目录迭代器,洗牌图像 洗牌(图像);,php,Php,你能帮我查一下上面的代码吗?我想使用DirectoryIterator将图像添加到数组中,然后对图像进行无序排列以生成随机图像。感谢您的宝贵意见 这取决于您对图像所做的操作,以及您当时所在的路径,但下面将把它们的完整路径名放入一个数组中 <?php $dir = new DirectoryIterator('../images/main_body_image/'); foreach ($dir as $file) { $images [] = array ( $file

你能帮我查一下上面的代码吗?我想使用DirectoryIterator将图像添加到数组中,然后对图像进行无序排列以生成随机图像。感谢您的宝贵意见

这取决于您对图像所做的操作,以及您当时所在的路径,但下面将把它们的完整路径名放入一个数组中

<?php


$dir = new DirectoryIterator('../images/main_body_image/');
foreach ($dir as $file)
{
    $images [] = array (
    $file->getPathname() . "\n";
    $file->getFilename(). "\n";
    );
}

?>

shuffle($images);

除了一些语法错误之外,您的代码似乎在正确的轨道上。你到底在哪里需要帮助?这就是你所需要的:
if($file->isFile())$images[]=$file->getPathname()还有你的代码
shuffle($images)
应该在关闭之前
?>
不要忘记排除
我不认为你会喜欢
这样的图像。我假设它是一个满是图像的文件夹,否则我认为
glob()
更有意义。我想随机显示图像。它当前显示的是路径名,而不是图像本身,我们是否需要包含getFilename();图像在浏览器中不明显。
<?php
$dir = new DirectoryIterator('../images/main_body_image/');
foreach($dir as $file){
  $images[] = $file->getPathname();
}
shuffle($images); $imgs = '';
foreach($images as $v){
  $imgs .= "<img scr='http://{$_SERVER['SERVER_NAME']}/$v' alt='randomImage' />";
}
//put the below wherever you want in your code
echo $imgs;
?>