Я пытаюсь перебрать файлы в каталоге и файлы в подкаталогах внутри:
<?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'>");
}
}
}
?>
Я, однако, получаю ошибку:
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;
дает:
Http: // Localhost / проекты / wordpress_projects / WP-содержание / темы / 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
Как я могу это исправить и заставить его работать правильно?
Спасибо всем заранее.
RecursiveDirectoryIterator::__construct
ожидает путь, а не Ури. Чтобы это исправить, попробуйте:
$dir = get_stylesheet_directory() . '/js'; // This gives you a path instead
Вы передаете URL, а вам нужно указать путь.
Проверьте это здесь: http://php.net/manual/en/class.recursivedirectoryiterator.php