PHP复制PHP模板文件并保存为新文件名

时间:2019-11-02 21:35:23

标签: php json

尝试使用以下PHP脚本从模板文件创建新的php文件。没有错误,但脚本不起作用。

<?php

//loop through stock.json
$stock = json_decode("../stock.json");
$stockValue = $stock["stock"];

try {
    for($i = 0; $i < sizeof($stockValue); $i++) {
        $stock_id = $stockValue[i][0];
        //read template
        $template = file_get_contents("../../product_pages/product_page-0000.php");
        //create new file
        if (!file_exists("../../product_pages/product-".$stock_id.".php")) {
            $handle = fopen("../../product_pages/product_page-".$stock_id.".php", "c+");
            fwrite($handle,$template); 
            fclose($handle); 
        }
    }
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
?>

该脚本应该打开“ product_page-0000”,然后使用新的文件名“ product_page- $ stock_id”保存其副本。 $ stock_id是从json文件“ stock.json”获得的。

该脚本当前未输出任何内容,并且没有错误代码。

0 个答案:

没有答案