简体   繁体   English

PHP的file_put_content覆盖

[英]php file_put_content overwrite

So i found this code which lets be write to a specific line 所以我找到了可以写到特定行的代码

  function SetSiteName(){
        global $session, $database, $form;
    $filepathname = "include/classes/constants.php";
    $target = 'sitename';
    $newline = 'define("sitename", "Testing.");';

    $stats = file($filepathname, FILE_IGNORE_NEW_LINES);   
    $offset = array_search($target,$stats) +32;
    array_splice($stats, $offset, 0, $newline);   
    file_put_contents($filepathname, join("\n", $stats)); 
    header("Location: ".$session->referrer);
   }

however it will not overwrite whats on that line it'll go to the next line and put the data in.. I'd like to make it overwrite what currently is on that line? 但是它不会覆盖该行上的内容,而是转到下一行并放入数据。.我想使其覆盖该行上的当前内容吗?

Any thoughts? 有什么想法吗?

You can overwrite a line of a file with this code. 您可以使用此代码覆盖文件的一行。

$filename = "file.txt";
$content = file_get_contents($filename);
$lines_array = explode(PHP_EOL, $content);

//overwrite the line that you want.
$lines_array[5] = "New text at line 6!";

file_put_contents($filename, implode(PHP_EOL, $lines_array));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM