Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php(xampp)从目录中随机获取文件_Php_Wordpress_Xampp_Wordpress Theming - Fatal编程技术网

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

使用php(xampp)从目录中随机获取文件,php,wordpress,xampp,wordpress-theming,Php,Wordpress,Xampp,Wordpress Theming,我正在构建一个wordpress主题,我正在尝试使用php以随机顺序从目录中获取所有jpg文件。。。。 我在win7(本地主机)上使用Xampp 代码如下: <? $dir = get_template_directory_uri().'/images/top/'; $file_display = array ('jpg', 'jpeg'); if(file_exists($dir) == false){ echo 'Directory \''

我正在构建一个wordpress主题,我正在尝试使用php以随机顺序从目录中获取所有jpg文件。。。。 我在win7(本地主机)上使用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, '" />';    
           }
       }
   } 
?>
我也试着改变

$dir =  get_template_directory_uri().'/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, '"  />';
                }
              }
            } 
  ?>

我就是这样做的

  <? 
            $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, '"  />';
                }
              }
            } 
  ?>


您是否有和
如果
前面(文件存在($dir)==false)
测试?是的,我编辑了问题,谢谢!我把它写在我的原始代码上了!尝试用
get\u template\u directory
替换
get\u template\u directory\u uri
,因为
get\u template\u directory\u uri
提供了一个uri(您不希望这样)@eaterofcastores从PHP5.0开始
文件\u存在
接受HTTP(以及其他一些协议)因此,只要OP使用PHP5,它就不重要了。使用get_template_directory()输出:C:\xampp\htdocs\Ni/wp content/themes/A/images/top/带混合斜杠(linux windows syle?)和i get directory''未找到您是否有和
如果
前面(文件存在($dir)==false)
测试?是的,我编辑了这个问题,非常感谢。我把它写在我的原始代码上了!尝试用
get\u template\u directory
替换
get\u template\u directory\u uri
,因为
get\u template\u directory\u uri
提供了一个uri(您不希望这样)@eaterofcastores从PHP5.0开始
文件\u存在
接受HTTP(以及其他一些协议)因此,只要OP使用PHP5,它就不重要了。使用get_template_directory()输出:C:\xampp\htdocs\Ni/wp content/themes/A/images/top/使用混合斜杠(linux windows syle?),通过使用get_template_directory()获取数组,我无法找到目录“”。并获取_template_directory_uri()以显示图像。感谢大家帮助我解决这个问题……请注意,在大多数情况下,如果您试图加载资产,最好使用
get\u stylesheet\u directory()
,而不是
get\u template\u directory()
,因为如果您使用子主题,前者将返回子主题的目录,而后者将返回父主题的目录,因此将导致您错过子主题资产。请使用get_template_directory()获取数组。并获取_template_directory_uri()以显示图像。感谢大家帮助我解决这个问题……请注意,在大多数情况下,如果您试图加载资产,最好使用
get\u stylesheet\u directory()
,而不是
get\u template\u directory()
,因为如果您使用子主题,前者将返回子主题的目录,而后者将返回父主题的目录,从而导致您丢失子主题资产。
  <? 
            $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, '"  />';
                }
              }
            } 
  ?>