如何用php替换文件的行

时间:2016-05-04 12:26:11

标签: php

所以,我需要让文件connect.php用false替换变量$ install = true(要替换的真正的iw ant)。

这是connect.php文件(我试过并搜索了互联网):

<?php
$db_servername = $_GET["servername"];
$db_username = $_GET["user"];
$db_password = $_GET["password"];

// Create connection
$conn = new mysqli($db_servername, $db_username, $db_password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}else{

    $myFile = "../config.php";
    $file = fopen("../config.php", "w+") or die("Error 404 file not found.");

    rewind($file);

    $contents = fread($file, filesize($myFile));
    $contents = str_replace('true','false', $contents, $count);
    fwrite($file, $contents);


    fclose($file);  

} 
?>

这是config.php文件,我希望将$ install的var info从true替换为false:

<?php
include("../connect.php");

// Create connection
$conn = mysqli_connect($db_servername, $db_username, $db_password);


// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$install = true;

?>

除了我的要求,请不要回答别的问题。就像你在我的代码中看到一个错误一样。只是忘了你看到它,我只是解决它。我真的需要更换方法。

1 个答案:

答案 0 :(得分:0)

不要在运行时更改程序代码。

相反,请将您的信息存储在

等文本文件中
file_put_contents('install-info.txt', '1');

并且这样读:

if (file_get_contents('install-info.txt') === '1') {
    ...
}