Jump to content

Writting text area to a txt file


qaq0

Recommended Posts

In simplest terms as your question was...

 


<form name="test" action="test.php" method="post">
<input type="textarea" name="test_text">Some Fun with Words!</textarea>
<input type="submit" name="submit" value="Submit">
</form>

test.php
<?

// Test for form submittal
if(isset($_REQUEST["submit"]))
{
     $text = $_REQUEST["test_text"];
    
     $file_h = fopen("yourfilepath/text.txt", "w");
     
     fwrite($file_h, $text, strlen($text));

     fclose();

     // All Done! Thank your user for their gracious input.
}

?>

Deathstar,

 

I didn't really understand the question you are asking. If you mean you have multiple chunks of text, and you want to insert them into the file, that is possible.

 

If you knew the specific place in the text file you wanted to insert, you would change the files beginning pointer using fseek().

 

For instance, if the file was 2000 bytes large, and you wanted to insert your text at the middle of the file, you would do this:

 


// Move file pointer to middle of the file
fseek($file_h, 1000);

// Write in the data
fwrite($file_h, $test, strlen($text));

// Close file
fclose($file_h);

Create a file in your directory called test.txt inside write what you want.

Create a file called edit2area.php or how you want call it.

 

<?php
$file = "test.txt"; // insert your filename or url here
if (!isset($_POST['submit']))
{
$fo = fopen($file, "r");
$fr = fread($fo, filesize($file));

echo "<form method='post' action='{$_SERVER['PHP_SELF']}'>
<textarea name='newfile' rows='10' cols='50'>{$fr}</textarea>
<p>
<input type='submit' name='submit' value='Save' />
</form>";
fclose($fo);
}
else
{
$fo = fopen($file, "w");
$fw = fwrite($fo, $_POST['newfile']);

fclose($fo);
}
?>

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.