最初,我将cf_replace()和另一个.php文件中的内部foreach()循环分开了。当我运行调用该代码的script.php时,它很好。现在我必须将我的两个php脚本合并为一个,不知何故,我的一个foreach()循环没有将cf_replace函数保留在目录中,而是将空文件吐出到我当前的目录中!我已经盯着这段代码了一段时间,并会欣赏一双新鲜的眼睛。
$ip = $argv[1];
// Declare vars - e.g. $currentTime
$htmlDir = "app/HtmlPages/$ip" ."-". "$currentTime/$ip/index.php/art";
...
// Make the directory where the generated HTML files will go.
mkdir( "app/HtmlPages/$ip" ."-". "$currentTime", 0777, false );
// Use wget to recurse over the entire website, downloading every webpage.
$wget_cmd = "wget -P app/HtmlPages/$ip"."-"."$currentTime/ \
--recursive \
--no-clobber \
--domains $ip \
http://$ip/index.php/art/art.htm";
exec ( $wget_cmd );
...
// Iterate through each Html file in $htmlDir and replace with ColdFusion
foreach( new DirectoryIterator( $htmlDir ) as $htm_file)
{
if( $htm_file->isDot () || !$htm_file->isFile ()) {
continue;
}
foreach( new DirectoryIterator ( './cf_templates/' ) as $cf_file )
{
// Checks for all files
if ($cf_file->isDot () || ! $cf_file->isFile ()) {
continue;
}
// Grabs the template name and content to replace
$cf_name = $cf_file->getBasename( $cf_file->getExtension() );
$cf_code = file_get_contents( './cf_templates/' . $cf_file );
cf_replace( $cf_name, $cf_code, $htm_file );
}
echo "Finished replacing Html code in $htm_file.\r\n";
}
...
function cf_replace( $name, $new_code, $source ) {
$content = file_get_contents( $source );
$start = preg_quote( "<!-- $name COLDFUSION BEGIN -->" , "#");
$end = preg_quote( "<!-- $name COLDFUSION END -->", "#" );
$needle_search = '#( $start )(.*?)( $end )#si';
$temp = preg_replace( $needle_search, $new_code, $content );
$temp = file_put_contents( $source, $temp );
return $temp;
}
我认为cf_replace()中的file_put_contents()只会将文件放回原始文件中 - 该文件保存在同一目录中......不是吗?任何帮助表示赞赏!
答案 0 :(得分:0)
循环遍历目录迭代器时返回的名称不包含目录名称。你需要添加它,所以调用
cf_replace($name, $cf_code, "$htmlDir/$htmFile");