Помогите дописать конвертор php в html
Есть код, который копирует из одной папки содержимое в другую. Так же есть код, который исходный php конвертирует в конечный, статический html. Хочу их объединить, но что то не получается.
Вот код, который все копирует.
function recursive_files_copy($source_dir, $destination_dir){
// Open the source folder / directory
$dir = opendir($source_dir);
// Create a destination folder / directory if not exist
@mkdir($destination_dir);
// Loop through the files in source directory
while($file = readdir($dir))
{
// Skip . and ..
if(($file != '.') && ($file != '..') && ($file != 'includes') && (pathinfo($file, PATHINFO_EXTENSION) != 'php'))
{
// Check if it's folder / directory or file
if(is_dir($source_dir.'/'.$file))
{
// Recursively calling this function for sub directory
recursive_files_copy($source_dir.'/'.$file, $destination_dir.'/'.$file);
}
else
{
// Copying the files
copy($source_dir.'/'.$file, $destination_dir.'/'.$file);
}
}
}
closedir($dir);
// convertphp();
}
$source_dir = "D:\openserver\domains\test1.com";
$destination_dir = "D:\openserver\domains\test2.com";
recursive_files_copy($source_dir, $destination_dir);
А вот код, которые конвертирует все в html:
ob_start();
include "../pf.com/index.php";
$php_to_html = ob_get_clean();
file_put_contents("index.html", $php_to_html);
Но в нем нужно вручную запрашивать файл и ставить его куда нужно. Хочу этот код внедрить в верхний, чтобы все делалось автоматически. Пытался делать так:
function recursive_files_copy($source_dir, $destination_dir){
// Open the source folder / directory
$dir = opendir($source_dir);
// Create a destination folder / directory if not exist
@mkdir($destination_dir);
// Loop through the files in source directory
while($file = readdir($dir))
{
// Skip . and ..
if(($file != '.') && ($file != '..') && ($file != 'includes') && (pathinfo($file, PATHINFO_EXTENSION) != 'php'))
{
// Check if it's folder / directory or file
if(is_dir($source_dir.'/'.$file))
{
// Recursively calling this function for sub directory
recursive_files_copy($source_dir.'/'.$file, $destination_dir.'/'.$file);
}
else
{
// Copying the files
copy($source_dir.'/'.$file, $destination_dir.'/'.$file);
}
//////////////
}else if(($file != 'includes') && (pathinfo($file, PATHINFO_EXTENSION) == 'php')){
ob_start();
include $file;
$file = ob_get_clean();
copy($source_dir.'/'.$file, $destination_dir.'/'.$file);
}
//////////////
}
closedir($dir);
// convertphp();
}
$source_dir = "D:\openserver\domains\test1.com";
$destination_dir = "D:\openserver\domains\test2.com";
recursive_files_copy($source_dir, $destination_dir);
Но где то допускаю ошибку.
Текст ошибки:
Warning: copy(D:\openserver\domains\test.com/
Warning: include(index.php): failed to open stream: No such file or directory in D:\OpenServer\domains\test2.com\copy.php on line 28
Warning: include(): Failed opening 'index.php' for inclusion (include_path='.') in D:\OpenServer\domains\test2.com\copy.php on line 28
): failed to open stream: No such file or directory in D:\OpenServer\domains\test2.com\copy.php on line 30
Ответы (1 шт):
Если вдруг кому то нужно будет, то итоговый результат вот. Если есть пожелания по улучшению или оптимизации, пишите.
Если вкратце, то скрипт берет и копирует все, кроме папки inludes(внутри header.php, footer.php и т д), и других скриптов для с расширением php, а это основные страницы(index.php, about.php, contacts.php).
Спросите для чего это? Все просто. Вы можете удобно работать со всеми повторяющимися элементами (header.php, footer.php и т д), просто добавив их в папку includes, и в самих страницах добавлять через:
<?php include 'includes/header.php' ?>
А затем, когда все будет готово, с помощью скрипта сгенерировать статический html/css/js сайт и загрузить его на тот же github pages. Важно понимать, что github pages не начнет понимать php, этот все просто чтобы облегчить разработку статического html/css/js сайта.
!!!Скрипт должен находиться в той же папке, где вы хотите, что бы в итоге у вас был ваш статический сайт!!!
Затем чтобы запустить скрипт, в консоли перейдите в папку со скриптом и запустите его с помощью команды :
php copy.php
<?php
$source_dir = "D:\openserver\domains\source.com";
$destination_dir = "D:\openserver\domains\destination.com";
recursive_files_copy($source_dir, $destination_dir);
function recursive_files_copy($source_dir, $destination_dir)
{
// Open the source folder / directory
$dir = opendir($source_dir);
// Create a destination folder / directory if not exist
@mkdir($destination_dir);
// Loop through the files in source directory
while ($file = readdir($dir))
{
// Skip . and ..
if (($file != '.') && ($file != '..') && ($file != 'includes') && (pathinfo($file, PATHINFO_EXTENSION) != 'php'))
{
// Check if it's folder / directory or file
if (is_dir($source_dir . '/' . $file))
{
// Recursively calling this function for sub directory
recursive_files_copy($source_dir . '/' . $file, $destination_dir . '/' . $file);
}
else
{
// Copying the files
copy($source_dir . '/' . $file, $destination_dir . '/' . $file);
}
}
else if ((!is_dir($source_dir . '/' . $file)) && (pathinfo($file, PATHINFO_EXTENSION) == 'php'))
{
ob_start();
include $source_dir . '/' . $file;
$php_to_html = ob_get_clean();
$fp = fopen($file, "w");
fwrite($fp, $php_to_html);
fclose($fp);
rename(pathinfo($file, PATHINFO_BASENAME) , pathinfo($file, PATHINFO_FILENAME) . '.html');
}
}
closedir($dir);
// convertphp();
}
?>