为什么我的 foreach() 循环将文件返回到我的当前目录?

Why is my foreach() loops returning files into my current directory?

最初,我在另一个 .php 文件中将我的 cf_replace() 和内部 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() 只会将文件放回原始文件中 - 原始文件保存在同一目录中...不是吗?感谢您的帮助!

循环目录迭代器时返回的名称不包含目录名称。你需要添加它,所以调用

cf_replace($name, $cf_code, "$htmlDir/$htmFile");