Jump to content

Text editor save to file


chloebyte
Go to solution Solved by Ch0cu3r,

Recommended Posts

For some reason its not saving input to the text file.

 

 

 

<form action="edit.php" method="post">


<textarea Name="update" cols="50" rows="10">
</textarea>Enter text here<br/>

<input type="submit" value="update"/>

</form>
<?php
$_POST['update'];
$file1 = "file.txt";
$file = fopen($file1, "w+");
 
$contents = file_get_contents($file1);
file_put_contents($file1, $contents);
if($_POST['update'])
fwrite($file, $file1);
 
 
fclose($file);
?>

anyone know why?;/

Edited by chloebyte
Link to comment
Share on other sites

  • Solution

1) You have not assigned this

$_POST['update'];

to a variable. I presume that line is meant to read as

$update = $_POST['update'];

2) Open the file use append mode

$file = fopen($file1, "a");

Then there is no need for this

$contents = file_get_contents($file1);
file_put_contents($file1, $contents);

3) fwrite takes the file handler as the first argument and the value to be written as the second argument (needs to be $update not $file1)

fwrite($file, $update);

The alternative is not use fopen/fwrite/fclose  but to use only file_put_content using the FILE_APPEND flag

if(isset($_POST['update']))
{
  $file = "file.txt";
  $update = $_POST['update'];
  file_put_contents($file, $update, FILE_APPEND); // append $update the file.txt
}
Link to comment
Share on other sites

 

1) You have not assigned this

$_POST['update'];

to a variable. I presume that line is meant to read as

$update = $_POST['update'];

2) Open the file use append mode

$file = fopen($file1, "a");

Then there is no need for this

$contents = file_get_contents($file1);
file_put_contents($file1, $contents);

3) fwrite takes the file handler as the first argument and the value to be written as the second argument (needs to be $update not $file1)

fwrite($file, $update);

The alternative is not use fopen/fwrite/fclose  but to use only file_put_content using the FILE_APPEND flag

if(isset($_POST['update']))
{
  $file = "file.txt";
  $update = $_POST['update'];
  file_put_contents($file, $update, FILE_APPEND); // append $update the file.txt
}

Thank you very much!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.