2017-07-28 73 views
0

寫在一個txt一定行,我想什麼:我想用PHP

file.txt: 
Name: sam 
email: [email protected] 

,所以我有這個

<?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個和一個提交按鈕i將設置文本爲$字符串,那麼這將去的TXT文件中的相應行。

+0

什麼的.TXT file___ – RiggsFolly

+0

構成___the正確的路線,請閱讀[我可以問什麼議題有關(http://stackoverflow.com/help/on-topic) 和[如何問的好問題](http://stackoverflow.com/help/how-to-ask) 和[完美問題](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question /) 以及如何創建一個[最小,完全和可驗證示例](http://stackoverflow.com/help/mcve) 並且還[採取遊](http://stackoverflow.com/tour) – RiggsFolly

+2

' //我應該在這裏做什麼'我們也是。 –

回答

0
//Your File name with extension 
$fileName = "file.txt"; 

//text you want to add in file 
$txt = "Name: sam" . PHP_EOL . "email: [email protected]" . PHP_EOL ; 

//Save file 
$myfile = file_put_contents($fileName , $txt.PHP_EOL , FILE_APPEND | LOCK_EX);