简体   繁体   中英

Writing text to a file [PHP, HTML]

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?

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 .

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM