[英]php file_put_content overwrite
所以我找到了可以寫到特定行的代碼
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);
}
但是它不會覆蓋該行上的內容,而是轉到下一行並放入數據。.我想使其覆蓋該行上的當前內容嗎?
有什么想法嗎?
您可以使用此代碼覆蓋文件的一行。
$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.