I'm trying to iterate through files in a directory and files in sub-directories within:
<?php
$dir = get_stylesheet_directory_uri() . '/js';
echo $dir;
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST );
foreach ( $iterator as $path ) {
if ($path->isDir()) {
print('<div> DIR : ' . $path . PHP_EOL . "</div>");
} else {
$path_parts = pathinfo($path);
$file_parts = $path_parts['extension'];
if ($file_parts == "js")
{
$array = explode("/opt/lampp/htdocs", $path);
unset($array[0]);
echo $path = implode("/", $array);
print("<div>http://localhost$path" . PHP_EOL . "</div>");
echo("<script src='http://localhost/$path'></script>");
} else if ($file_parts == "css") {
echo("<script src='http://localhost/$path'></script>");
echo("<link rel='stylesheet' href='http://localhost/$path'>");
}
}
}
?>
I, however, get the error:
Fatal error: Uncaught UnexpectedValueException: RecursiveDirectoryIterator::__construct(http://localhost/projects/wordpress_projects/wp-content/themes/rev_cust/js): failed to open dir: not implemented in /opt/lampp/htdocs/projects/wordpress_projects/wp-content/themes/rev_cust/header.php:40
echo $dir;
gives:
http://localhost/projects/wordpress_projects/wp-content/themes/rev_cust/js
The StackTrace:
Fatal error: Uncaught UnexpectedValueException: RecursiveDirectoryIterator::__construct(http://localhost/projects/wordpress_projects/wp-content/themes/rev_cust/js): failed to open dir: not implemented in /opt/lampp/htdocs/projects/wordpress_projects/wp-content/themes/rev_cust/header.php:40 Stack trace: #0 /opt/lampp/htdocs/projects/wordpress_projects/wp-content/themes/rev_cust/header.php(40): RecursiveDirectoryIterator->__construct('http://localhos...') #1 /opt/lampp/htdocs/projects/wordpress_projects/wp-includes/template.php(684): require_once('/opt/lampp/htdo...') #2 /opt/lampp/htdocs/projects/wordpress_projects/wp-includes/template.php(643): load_template('/opt/lampp/htdo...', true) #3 /opt/lampp/htdocs/projects/wordpress_projects/wp-includes/general-template.php(45): locate_template(Array, true) #4 /opt/lampp/htdocs/projects/wordpress_projects/wp-content/themes/rev_cust/index.php(18): get_header() #5 /opt/lampp/htdocs/pro in /opt/lampp/htdocs/projects/wordpress_projects/wp-content/themes/rev_cust/header.php on line 40
How can I fix it and make it work right?
Thank you all in advance.
RecursiveDirectoryIterator::__construct
expects a path not a uri. To fix this try:
$dir = get_stylesheet_directory() . '/js'; // This gives you a path instead