PHP包含github页面的函数



我有一个php网站,从php我只使用包括,使其更容易内联重复元素(页眉/页脚)。我想输出一个静态的html网站,我可以很容易地上传到github页面。

是难以实现还是可能实现?

乌利希期刊指南将答案移到单独的注释中。

此代码将php网站目录中的所有内容(除了包含文件夹和所有php扩展名的文件)复制到另一个任意文件夹。所有扩展名为php的文件都被转换为html格式(includes文件夹除外)。

问它是干什么用的?这很简单。您可以方便地处理所有重复元素(header.php, footer.php等),只需将它们添加到includes文件夹中,并在页面本身中通过:

添加<?php include 'includes/header.php'?>

然后,当一切准备就绪时,使用脚本生成静态html/css/js站点并将其上传到github页面。重要的是要明白github页面不会开始理解php,这一切都只是为了方便开发静态html/css/js网站。

! !脚本必须在您想要的相同文件夹中,以便您最终获得静态站点!!!

要运行脚本,进入脚本在控制台中所在的目录,写入:

php copy.php
<?php
$source_dir = "D:openserverdomainssource.com";
$destination_dir = "D:openserverdomainsdestination.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();

}
?>

最新更新