使用php (xampp)以随机顺序从目录中获取文件


Get files from a directory in random order using php (xampp)

我正在构建一个wordpress主题,我正试图从使用php....随机顺序的目录中获取所有JPG文件我在win7 (localhost)上使用Xampp。

这是代码:

<? 
    $dir =  get_template_directory_uri().'/images/top/';
    $file_display = array ('jpg', 'jpeg');
    if(file_exists($dir) == false){
        echo 'Directory ''', $dir, ''' not found';
    } else {
       $dir_contents = scandir($dir);
       shuffle($dir_contents);
       foreach ($dir_contents as $file) {
           $file_type = strtolower(end(explode('.', $file)));
           if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true){ 
                echo '<img  src="', $dir, '/', $file, '" alt="', $file, '" />';    
           }
       }
   } 
?>

i always get

Directory 'http://localhost/ni/wp-content/themes/A/images/top/' not found 

i also tried change

$dir =  get_template_directory_uri().'/images/top/';

:

    $dir = "C:'xampp'htdocs'Ni'wp-content'themes'A'images'top'";

但仍然没有运气,任何帮助将不胜感激!

我是这样做的。

  <? 
            $dir =  get_template_directory().'/images/top';
            $imageDir= get_template_directory_uri().'/images/top';
            $file_display = array ('jpg', 'jpeg');
            if (file_exists($dir) == false) {
              echo 'Directory ''', $dir, ''' not found';
            } else {
              $dir_contents = scandir($dir);
              shuffle($dir_contents);
              foreach ($dir_contents as $file) {
                $file_type = strtolower(end(explode('.', $file)));
                if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) {
                  echo '<img src="', $imageDir, '/', $file, '"  />';
                }
              }
            } 
  ?>