我想使用 php 写入 txt 上的某一行



我想要的:

file.txt:
Name: sam
email: email@email.com

所以我有这个

<?php
$myFile = "file.txt";
$txt = 'thetxt';
$file = file($myFile);
foreach ($file as $row_num => $row) {
//idk what i should do here
}
$file_o = fopen($myFile, "w");
fwrite($file_o, $txt);
fclose($file_o);
?>

基本上我想要 2 个,并使用提交按钮将文本设置为$string然后这将转到 txt 文件上的相应行。

//Your File name with extension
$fileName = "file.txt";
//text you want to add in file
$txt = "Name: sam" . PHP_EOL . "email: email@email.com" . PHP_EOL ;
//Save file
$myfile = file_put_contents($fileName , $txt.PHP_EOL , FILE_APPEND | LOCK_EX);

最新更新