You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP的file_put_contents()函数中直接写入HTML代码并解决file_get_contents报错问题

解决file_put_contents直接写入HTML/PHP代码的问题

嘿,我一眼就看出问题所在了——你错误地把file_get_contents()用在了HTML代码字符串上!这个函数的作用是读取文件内容,你把拼接好的代码字符串传给它,它会把这个字符串当成文件路径去查找,自然会提示找不到文件。

其实你完全不需要file_get_contents()这一步,直接把你拼接好的代码内容传给file_put_contents()就行。另外还有个小细节要调整:你注释掉的$header = include 'style/header.php';不能直接用include赋值,得用输出缓冲来捕获include的内容。

下面是修正后的完整代码:

if (isset($_POST["category"])) {
    $Category = $_POST["category"];
    $ProductName = $_POST["productname"];
    $Description = $_POST["description"];
    $optitle = $_POST["optitle"];
    $op1 = $_POST["op1"];
    $op2 = $_POST["op2"];
    $op3 = $_POST["op3"];
    $price = $_POST["Price"];
    $productno = "SELECT productno FROM productno";
    
    // 用输出缓冲捕获header.php的内容
    ob_start();
    include 'style/header.php';
    $header = ob_get_clean();

    $file_destination = 'Categorys/products/4.php';
    $script = '<?php '.$header.' ?>
    <link rel="stylesheet" type="text/css" href="style/style.css" />
    <link rel="stylesheet" type="text/css" href="style/style2.css" />
    <main class="container">
        <!-- Left Column / Headphones Image -->
        <div class="left-column">
            <img data-image="black" src="../../images/hammer.png" alt="">
            <img data-image="blue" src="../../images/hammer.png" alt="">
            <img data-image="red" class="active" src="../../images/hammer.png" alt="">
        </div>
        <!-- Right Column -->
        <div class="right-column">
            <!-- Product Description -->
            <div class="product-description">
                <span>'.$Category.'</span>
                <h1>'.$ProductName.'</h1>
                <p>'.$Description.'</p>
            </div>
            <!-- Product Configuration -->
            <div class="product-configuration">
                <!-- Cable Configuration -->
                <div class="cable-config">
                    <span>'.$optitle.'</span>
                    <div class="cable-choose">
                        <button>'.$op1.'</button>
                        <button>'.$op2.'</button>
                        <button>'.$op3.'</button>
                    </div>
                </div>
            </div>
            <!-- Product Pricing -->
            <div class="product-price">
                <span>'.$price.'</span>
                <a href="#" class="cart-btn">Add to cart</a>
            </div>
        </div>
    </main>';
    
    // 直接将拼接好的代码写入目标文件
    file_put_contents($file_destination, $script);
    
    $sql = "UPDATE productno SET productno = productno + 1";
    $result = mysqli_query($link, $sql);
}

关键修正说明:

  • 移除了多余的$content = file_get_contents($script);,直接用file_put_contents($file_destination, $script)完成写入操作
  • 用ob_start()和ob_get_clean()实现输出缓冲,正确捕获header.php的内容,让$header变量能包含目标文件的代码
  • 清理了字符串中的转义引号,让代码更易读

这样修改后,就能直接把你拼接好的HTML和PHP代码写入目标文件,不会再出现找不到文件的错误了。

内容的提问来源于stack exchange,提问作者Will

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 06:56:36