11

After I've read a lot of similar problems with the edit/update function on a file and none of it worked I would like to ask for some help.

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

  1. This was the last code which I've 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);
    
  2. And this is my previous try:

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

I hope someone would explain me how to do this the right way. Thank you.

This is all of my 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 to add something which might be useful:
I am getting this error which I can't manage to find an answer for with Google.
Warning: fopen(text/) [function.fopen]: failed to open stream: Is a directory in

Sam
  • 381
  • 3
  • 15
Mr Andreev
  • 111
  • 1
  • 1
  • 6
  • Use `ini_set('display_errors', 1);` and `error_reporting(E_ALL);` at the top of your code so that errors are displayed, then you may be able to see why it is not working. – Matt Sep 18 '13 at 06:49
  • You may be doing it right; but may not have permissions. Please follow up with http://stackoverflow.com/questions/9319619/php-and-file-write-permissions . Hope it helps. – Akshaya Shanbhogue Sep 18 '13 at 06:50
  • I've set the permissions on the folder to be 777 because I would like to see if it is working or not and still doesn't – Mr Andreev Sep 18 '13 at 08:05

6 Answers6

7

You just need to change :

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

to this :

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

The concatenation operator in PHP is . (not +)

And make sure the directory text exists, otherwise its better to check 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);
Nikhil Patel
  • 1,761
  • 12
  • 23
  • Well this errors came up: Warning: fopen(text/) [function.fopen]: failed to open stream: Is a directory in edit.php on line 120 Warning: fwrite(): supplied argument is not a valid stream resource in edit.php on line 121 Warning: fclose(): supplied argument is not a valid stream resource in edit.php on line 122 – Mr Andreev Sep 18 '13 at 07:38
  • Your `$row['name']` is turning out to be blank. Check that. – Nikhil Patel Sep 18 '13 at 07:40
  • I've just decided to use it so to be easy with the document. I've had subject and content on the database and on the subject is the path to the document so i've decided to try it with a name as well. So what should I do delete that? – Mr Andreev Sep 18 '13 at 08:26
  • You are saying that `$_POST['subject']` is the path to the document ? Show your complete code, that should help. – Nikhil Patel Sep 18 '13 at 10:01
7

You can also open file in append mode using fopen() and put whatever you have at the end like

$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);
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
5

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

Moeed Farooqui
  • 3,604
  • 1
  • 18
  • 23
  • I've define the file_get_content on a previous part of the document so I could get the infomation and diplay in on the textarea – Mr Andreev Sep 18 '13 at 07:40
2

I prefer to use file_put_contents and set flags in order to add text instead of overwrite the whole file. With flags you can also lock the file if you have multiple script accessing the same file simultaneously.

Here is how:

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

Here is the full documentation: https://www.php.net/manual/en/function.file-put-contents.php

Pinonirvana
  • 920
  • 1
  • 8
  • 12
0

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 recommand to protect this with hmtlentities or anything else as it's a direct user input.

  • Can't it just be: $data_to_write = $_POST['subject']; – Loko Sep 18 '13 at 07:00
  • Usually the reason for appending an empty string to an input variable is to make the code handle the case where the input var is undefined. – MarkHu Mar 28 '15 at 00:39
0

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