How to edit / update txt file with php

after I read a lot of similar problems with the edit / update function in the file and did not work, I would like to ask for some help.

I am trying to edit a .txt document with php. I tried these things:

This was the last code I read here and didn't work.

$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); 

And this is my preview attempt:

 $new_contents = "$_POST[subject]\n"; $path = "text/$row[name]"; file_put_contents($path, $new_contents); 

Hope someone will explain to me how to do it right. Thanks.

Here is the whole code

 <?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>' ; } ?> 

Just add something that might be helpful. I get this error, which I cannot find the answer in google. Warning: fopen (text /) [function.fopen]: could not open the stream: is there a directory in

+4
source share
5 answers

You just need to change:

 $file_path = "text/" + $row['name']; 

:

 $file_path = "text/" . $row['name']; 

Concatenation operator in PHP . (not + )

And make sure that the text directory exists, otherwise it is better to check it and then write:

 $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); 
+3
source

You need to use file_get_contents to get the text of your file.

 $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); 

See Documentation

+1
source

I think the problem may be the first line:

 $data_to_write = "$_POST[subject]"; 

Replace it with the following:

 $data_to_write = "" . $_POST['subject']; 

And I highly recommend protecting this with hmtlentities or anything else as this is direct user input.

0
source

You can also open the file in add mode with the fopen () function and put everything you have at the end, for example

 $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); 

Read more on the TechFlirt File Handling Blog here.

0
source

I just entered the code into the compiler, you are missing } at the end.

0
source

Source: https://habr.com/ru/post/1502733/


All Articles