【问题标题】:is there a way to put html code in a file_put_contents() function?有没有办法将 html 代码放入 file_put_contents() 函数中?
【发布时间】:2021-08-19 19:18:06
【问题描述】:

它只是说:

file_get_contents("lot of html"):打开流失败:第 88 行的 C:\xampp\htdocs\timups-html\admin.php 中没有这样的文件或目录

有没有办法将 HTML 放入函数中而不会出错? 这是我的代码:

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 = include 'style/header.php';
  $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>';

  $content = file_get_contents($script);
  file_put_contents('Categorys/products/4.php', $content);

$sql = "UPDATE productno SET productno = productno + 1"
$result = mysqli_query($link, $sql);
}

【问题讨论】:

  • file_get_contents 将读取一个文件 - 不像你这里的字符串,所以file_put_contents('Categorys/products/4.php', $script); 可能更合适
  • 你到底为什么需要file_get_contents?预期的结果是什么?
  • 您是否意识到您在浪费人们的时间只是因为您使用了一个功能,甚至没有花时间阅读手册?

标签: php html sql function variables


【解决方案1】:

你可以通过file_put_contents放任何你喜欢的字符串内容。

您无需在之前发送file_get_contents。我不知道你想用这条线实现什么。请这样尝试:

//$content = file_get_contents($script); // remove this line
file_put_contents('Categorys/products/4.php', $script);

您可以调用file_get_contents($path_to_file) 将现有文件的内容加载到变量中。您传递给该函数的参数必须是现有文件的本地路径。 请阅读docs for file_get_contents

您可以调用file_put_contents 将一些数据写入可能已经存在或不存在的文件中。如果没有,它将被创建。 请阅读docs for file_put_contents

【讨论】:

    猜你喜欢
    • 2021-03-12
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    相关资源
    最近更新 更多