3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<html> <head> <title>Write to a text file</title> </head> <body> <h1>Adding a text block to a text file:</h1> <form action="myfile.php" method='post'> <textarea name='textblock'></textarea> <input type='submit' value='Add text'> </form> <?php // Open the text file $f = fopen("textfile.txt", "w"); // Write text fwrite($f, $_POST["textblock"]); // Close the text file fclose($f); // Open file for reading, and read the line $f = fopen("textfile.txt", "r"); // Read text echo fgets($f); fclose($f); ?> </body> </html>
based on LDN0D
Output for 7.3.0 - 7.3.1
<html> <head> <title>Write to a text file</title> </head> <body> <h1>Adding a text block to a text file:</h1> <form action="myfile.php" method='post'> <textarea name='textblock'></textarea> <input type='submit' value='Add text'> </form> Warning: fopen(): open_basedir restriction in effect. File(textfile.txt) is not within the allowed path(s): (/tmp:/in:/etc) in /in/094ca on line 16 Warning: fopen(textfile.txt): failed to open stream: Operation not permitted in /in/094ca on line 16 Notice: Undefined index: textblock in /in/094ca on line 19 Warning: fwrite() expects parameter 1 to be resource, bool given in /in/094ca on line 19 Warning: fclose() expects parameter 1 to be resource, bool given in /in/094ca on line 22 Warning: fopen(): open_basedir restriction in effect. File(textfile.txt) is not within the allowed path(s): (/tmp:/in:/etc) in /in/094ca on line 25 Warning: fopen(textfile.txt): failed to open stream: Operation not permitted in /in/094ca on line 25 Warning: fgets() expects parameter 1 to be resource, bool given in /in/094ca on line 28 Warning: fclose() expects parameter 1 to be resource, bool given in /in/094ca on line 29 </body> </html>
Output for 7.1.25 - 7.2.13
<html> <head> <title>Write to a text file</title> </head> <body> <h1>Adding a text block to a text file:</h1> <form action="myfile.php" method='post'> <textarea name='textblock'></textarea> <input type='submit' value='Add text'> </form> Warning: fopen(): open_basedir restriction in effect. File(textfile.txt) is not within the allowed path(s): (/tmp:/in:/etc) in /in/094ca on line 16 Warning: fopen(textfile.txt): failed to open stream: Operation not permitted in /in/094ca on line 16 Notice: Undefined index: textblock in /in/094ca on line 19 Warning: fwrite() expects parameter 1 to be resource, boolean given in /in/094ca on line 19 Warning: fclose() expects parameter 1 to be resource, boolean given in /in/094ca on line 22 Warning: fopen(): open_basedir restriction in effect. File(textfile.txt) is not within the allowed path(s): (/tmp:/in:/etc) in /in/094ca on line 25 Warning: fopen(textfile.txt): failed to open stream: Operation not permitted in /in/094ca on line 25 Warning: fgets() expects parameter 1 to be resource, boolean given in /in/094ca on line 28 Warning: fclose() expects parameter 1 to be resource, boolean given in /in/094ca on line 29 </body> </html>
Output for 5.6.0 - 7.1.7
<html> <head> <title>Write to a text file</title> </head> <body> <h1>Adding a text block to a text file:</h1> <form action="myfile.php" method='post'> <textarea name='textblock'></textarea> <input type='submit' value='Add text'> </form> Warning: fopen(textfile.txt): failed to open stream: Permission denied in /in/094ca on line 16 Notice: Undefined index: textblock in /in/094ca on line 19 Warning: fwrite() expects parameter 1 to be resource, boolean given in /in/094ca on line 19 Warning: fclose() expects parameter 1 to be resource, boolean given in /in/094ca on line 22 Warning: fopen(textfile.txt): failed to open stream: No such file or directory in /in/094ca on line 25 Warning: fgets() expects parameter 1 to be resource, boolean given in /in/094ca on line 28 Warning: fclose() expects parameter 1 to be resource, boolean given in /in/094ca on line 29 </body> </html>