[英]How to edit/update a txt file with php
在我閱讀了許多關於文件的編輯/更新功能的類似問題並且沒有一個工作后,我想尋求一些幫助。
我正在嘗試從 php 編輯.txt
文檔。 我試過這些東西:
這是我在這里閱讀的最后一個代碼,但沒有用。
$data_to_write = "$_POST[subject]"; $file_path = "text/" + $row['name']; $file_handle = fopen($file_path, 'w'); fwrite($file_handle, $data_to_write); fclose($file_handle);
這是我之前的嘗試:
$new_contents = "$_POST[subject]\\n"; $path = "text/$row[name]"; file_put_contents($path, $new_contents);
我希望有人能解釋我如何以正確的方式做到這一點。 謝謝你。
這是我所有的代碼:
<?php
if(isset($_GET['id']))
{
$edit = mysql_query("SELECT * FROM text_area WHERE text_id=$_GET[id]");
$row = mysql_fetch_assoc($edit);
$contents = file_get_contents($row['content']);
?>
<form action="" name="form" method="post">
<input type="hidden" name="id" value="<?php echo $row['text_id']; ?>" /><br />
<label for="">Заглавие:</label><br />
<input type="text" name="title" style="width:500px;" value="<?php echo $row['subject'] ?>" /><br />
<select name="opt">
<option value="0"></option>
<?php
$result = mysql_query("SELECT * FROM image_area");
while ($row = mysql_fetch_array($result))
{
echo "<option value=" . $row['path'] . ">" . $row['name'] . "</option>
";
}
?>
</select><input type="button" name="sumbitP" value="Choose" onclick="addtext();" /><a href="../image_list.php" target="_blank">Image list</a><br />
<textarea rows="10" cols="50" name="text" id="markItUp"><?php echo $contents ?></textarea><br />
<input type="submit" name="sumbitT" value="Update" />
<input type="reset" value="Reset" />
</form>
<?php
}
?>
<?php
if(isset($_POST['id']))
{
if(mysql_query("UPDATE text_area SET title='$_POST[subject]' WHERE text_id ='$_POST[id]'"))
{
$data_to_write = "" . $_POST['text'];
$file_path = "text/$row[name]";
$file_handle = fopen($file_path, 'w');
fwrite($file_handle, $data_to_write);
fclose($file_handle);
echo '<br><br><p align="center">Everything is ok</p>';
} else {
echo '<br><br><p align="center">Everything is not ok</p>' ;
}
?>
只是添加一些可能有用的東西:
我收到此錯誤,我無法通過 Google 找到答案。
Warning: fopen(text/) [function.fopen]: failed to open stream: Is a directory in
你只需要改變:
$file_path = "text/" + $row['name'];
對此:
$file_path = "text/" . $row['name'];
PHP 中的連接運算符是.
(不是+
)
並確保目錄text
存在,否則最好檢查然后寫入:
$data_to_write = $_POST['subject'];
$file_path = "text/" . $row['name'];
if ( !file_exists("text") )
mkdir("text");
$file_handle = fopen($file_path, 'w');
fwrite($file_handle, $data_to_write);
fclose($file_handle);
您還可以使用 fopen() 以追加模式打開文件,並將您擁有的任何內容放在最后,例如
$path = dirname(__FILE__).'/newfile.txt';
$fp = fopen($path, 'a');
if(!$fp){
echo 'file is not opend';
}
fwrite($fp, 'this is simple text written');
fclose($fp);
您需要使用file_get_contents
來獲取文件的文本。
$file_path= "text/" . $row['name'];
// Open the file to get existing content
$current = file_get_contents($file_path);
// Append a new person to the file
$data_to_write.= $_POST[subject]."\n";
// Write the contents back to the file
file_put_contents($file_path, $data_to_write);
查看文檔
我更喜歡使用 file_put_contents 並設置標志以添加文本而不是覆蓋整個文件。 如果您有多個腳本同時訪問同一個文件,您還可以使用標志鎖定文件。
方法如下:
$file_name = 'text.txt';
$line= "This is a new line\n";
// use flag FILE_APPEND to append the content to the end of the file
// use flag LOCK_EX to prevent other scripts to write to the file while we are writing
file_put_contents($file_name , $line, FILE_APPEND | LOCK_EX);
這是完整的文檔: https : //www.php.net/manual/en/function.file-put-contents.php
我認為問題可能出在第一行:
$data_to_write = "$_POST[subject]";
將其替換為以下內容:
$data_to_write = "" . $_POST['subject'];
我強烈建議使用 hmtlentities 或其他任何東西來保護它,因為它是直接的用戶輸入。
我只是將代碼放入編譯器中,最后您缺少一個}
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.