Tuesday, April 22, 2025

PHP Write, Append to Files

You are advised to check corresponding YouTube video at the end of this article. 


<?php

$result = fopen("new_file.txt" , "w") or 
die ("Unable to create that file");

fwrite($result, "Write this to file \n");

fclose($result);

?>

This code creates a new file named "new_file.txt" and opens it for writing, using the fopen() function with the "w" mode, which means that the file will be created if it doesn't exist, or truncated to zero length if it already exists.

If the file cannot be created or opened for writing, the or die() statement is executed, which will print the specified error message ("Unable to create that file") and terminate the script immediately.

The fwrite() function is then used to write the string "Write this to file" to the opened file handle ($result). The "\n" at the end of the string represents a newline character, which will create a new line in the file.

Finally, the fclose() function is used to close the file handle, which flushes any pending writes to disk and releases the system resources associated with the file. 


<?php

$result = fopen("new_file.txt" , "a") or 
die ("Unable to create that file");

fwrite($result, "Append this to file \n");

fclose($result);

?>

This PHP code opens a file named "new_file.txt" and appends the string "Append this to file" followed by a newline character to the end of the file.

The fopen function is used to open the file in "append" mode, which means that data will be written to the end of the file instead of overwriting the existing content. The "a" mode flag is used to open the file in append mode.

Then, the fwrite function writes the string "Append this to file" followed by a newline character to the file. The file is then closed using the fclose function.

This code is useful for adding new content to an existing file without overwriting the existing data in the file.

form.html to pass things to .php file


<form action="grabthings.php" method="post">
<label>New Line:</label>
<input type="text" name="new_line">
<br><br>

<input type="submit" value="Submit">
</form>

This code creates an HTML form with one text input field named "new_line" and a submit button. When the user fills out the input field and clicks the submit button, the form data is sent to a PHP script called "grabthings.php" using the HTTP POST method. The value entered in the input field will be accessible in the PHP script using the $_POST superglobal variable.

grabthings.php


<?php 

$get_new_line = $_POST["new_line"];

$out = fopen("output.txt", "a") or 
die ("Can't append to output.txt");

fwrite($out, $get_new_line . "\n");

fclose($out);

?>

The value of the "new_line" input field is obtained using the $_POST superglobal variable and stored in the $get_new_line variable. The code then opens the "output.txt" file in append mode using the fopen() function, which creates the file if it does not exist. If the file cannot be opened, the script terminates and outputs an error message.

The fwrite() function is used to write the value of $get_new_line to the end of the "output.txt" file. The "\n" character is added to create a new line in the file.

The fclose() function is used to close the file handle and release the resources associated with it.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...