PHP - >从两个不同的文件夹中随机获取两个随机文件

时间:2014-01-27 18:37:20

标签: php arrays image

所以我想要实现的是获得两个随机图像文件,它们将成为两个不同文件夹的缩略图。将使用函数randomThumbnail($ dirs)调用这两个图像 $ dirs是一个包含两个不同文件夹位置的数组。提前致谢! 我做错了什么?是问题。

function randomThumbnail($albums_location){ #TODO: finish this function

 //$images = scandir($albums_location); old ver
 //$i = rand(2, sizeof($images)-1); old ver
$images = glob($albums_location . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

$randomImage = $images[array_rand($images)];    
    return $randomImage;
}

抱歉不清楚..我只是把代码清楚地告诉你们,然后把它们全部转移到班级

$y = $_GET['year'];  
$dir_photos = 'photos/'
$folders = scandir($dir_photos.$y);
foreach($folders as $albumFolder){
    if($albumFolder == '.' OR $albumFolder == '..'){
     $albumFolder = '';
    } else {
      $thumbnails = $this->randomThumbnail($dir_photos.$y.'/'.$albumFolder);
          echo '<img src="'.$thumbnails.'" />';
    }
}       

1 个答案:

答案 0 :(得分:0)

有可能glob()函数没有正确列出文件,因为路径没有以斜线结束。

仔细检查路径。

[编辑] 检查这一行:

$thumbnails = $this->randomThumbnail($dir_photos.$y.'/'.$albumFolder);

在此行之前创建一个var_dump:

var_dump($dir_photos.$y.'/'.$albumFolder);

而不是:

$thumbnails = $this->randomThumbnail($dir_photos.$y.'/'.$albumFolder);

尝试:

$thumbnails = $this->randomThumbnail($dir_photos.$y.'/'.$albumFolder.'/');
相关问题