0

I want to write text to the bottom of an existing text file but don't have a clue how to do it.

I have tried the code below but it don't work. The text file location is C:\inetpub\wwwroot\test.txt

<?php

    if(isset($_POST['submit'])){
        $email = $_POST['email'];
        $file = fopen("C:\inetpub\wwwroot\test.txt\\","a+");
        fwrite($file,$email);
        fclose($file); 
        print_r(error_get_last());
    }
?>

<form action= "" method="post" name="form">
<input type="text" name="email">
<br>
<br>
<input type="submit" name="submit" value="submit"><br>
</form>

What am I doing wrong?

2
  • Do you get any errors from the PHP interpreter when running this script? Why do you have two backslashes at the end of the file path? Commented Mar 10, 2014 at 0:07
  • The backslashes should either be doubled up (\\) to avoid escaping, or replaced with forwards slashes (/). The ones at the end look wrong too, as Kyle says. Commented Mar 10, 2014 at 1:05

2 Answers 2

3

Try

file_put_contents("file_path", your_content, FILE_APPEND);

so in your case...

<?php
    if(isset($_POST['submit'])) {
        $email = $_POST['email'];
        file_put_contents("C:\inetpub\wwwroot\test.txt", $email, FILE_APPEND);
    }
?>

Documentation here.

Sign up to request clarification or add additional context in comments.

5 Comments

sorry I'm new to PHP, where exactly would that go?
See edit. It just replaces your fopen, write, and close calls.
Oh I tried that but It didnt work, thats why I wondered where it went because maybe I done it wrong? But it dont work
Make sure your path is correct. Documentation for this function can be found here with examples: us2.php.net/file_put_contents
It's not working for some reason and if I was to add upload.php to the form action"" and put the php in the upload.php it goes to a white page.
0
<?php
if (isset($_POST['submit'])) {
  $email = $_POST['email'];
  $write = fopen('C:\inetpub\wwwroot\test.txt', 'a');
  fwrite($write, 'Email: '.$email ."\n");
  fclose($write);
}
?>

<form method="POST" name="form">
<input type="text" name="email"><br><br>
<input type="submit" name="submit" value="submit"><br>
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.