WordPress failed to open directory error to read folder

613 views Asked by At

Hi I have written code for taking images from a folder in WordPress but the path doesn't seem to be working. Here is my code. In my theme folder there is images and then empl folder but it gives error message "failed to open dir: No such file or directory in", I even tried server document root of php but it is not working. My file resides in page-template directory.

$string =array();
$filePath=bloginfo('template_url').'/images/empl';
echo $filePath;  
$dir = opendir($filePath);
echo $dir;
while ($file = readdir($dir)) { 
   if (preg_match("/.png/",$file) || preg_match("/.jpg/",$file) || preg_match("/.gif/",$file) ) { 
   $string[] = $file;
   }
}
$i=0;
while (sizeof($string) != 0 ){
    echo $i;
  $img = array_pop($string);
  echo "<div class='employee'><img src='$filePath$img'  data-src='$filePath$img'></div>";

  if($i>24)break;
  $i++;
}
2

There are 2 answers

0
JP Lew On

you have to use get_bloginfo() instead of bloginfo(), like this:

$filePath=get_bloginfo('template_url').'/images/empl';

Why? From the codex:

[bloginfo()] always prints a result to the browser. If you need the values for use in PHP, use get_bloginfo().

0
user2477139 On

i found solution using

$filePath=get_template_directory().'/images/empl';