2017-02-18 89 views
-1

我想要echo輸出並追加到使用php的現有文件。但我得到錯誤。如果我從腳本中刪除「>> master_yml.html」,則屏幕很好。請指教。謝謝如何echo輸出並使用PHP追加到另一個文件

<?php 

$filename = './workspace/vars1.txt'; 
$contents = file($filename); 

foreach($contents as $line) { 

     echo "<TR><TD>$line:</TD><TD> <input type=\"text\"name=\"$line\"size=\"30\" value= $line ></TD> </TR> " >> master_yml.html ; 
    } 

    ?> 
+0

這不是'bash'。你沒有輸出重定向('>>')。相反,你需要使用['file_put_contents'](https://secure.php.net/manual/en/function.file-put-contents.php)或者[fopen'和'fwrite'的組合] ](https://secure.php.net/manual/en/function.fwrite.php)。 – Bytewave

+0

你能舉一個例子嗎? thx – Sean

+0

只是......'echo'字符串,然後'fwrite'相同的字符串文件?我提供了文檔鏈接。 – Bytewave

回答

0

使用combination of fopen() and fwrite()

對於示例:

<?php 

$filename = './workspace/vars1.txt'; 
$contents = file($filename); 
$file = fopen('master_yml.html', 'a'); 

foreach ($contents as $line) { 
    $str = "<TR><TD>$line:</TD><TD> <input type=\"text\"name=\"$line\"size=\"30\" value= $line ></TD> </TR> "; 
    echo $str; 
    fwrite($file, $str); 
} 

fclose($file); 
+0

是的,我沒有分配字符串。但是,這解決了這個問題。太感謝了。 – Sean

+0

沒問題。不要忘記將答案標記爲已接受。 :) – Bytewave

相關問題