PHP File Create/Write

In PHP, you can create and write to files using various functions like fopen(), fwrite(), and fclose().


PHP Create File - fopen()


The fopen() function in PHP can be used to create a new file, Maybe a little confusing, but in PHP, a file is created using the same function used to open files.


The fopen() create a new file if it doesn't exist, depending on the mode you choose. It is often used for file handling tasks like creating, reading, writing, or appending data.


The example below creates a new file called "testfile.txt". The file will be created in the same directory where the PHP code resides:


Syntax of fopen()


fopen(filename, mode)



Parameters:

  • filename → The name of the file to be opened or created.
  • mode → Specifies the mode for opening the file (e.g., read, write, append, etc.).
$myfile = fopen("newfile.txt", "w")

PHP File Permissions


If you are having errors when trying to get this code to run, check that you have granted your PHP file access to write information to the hard drive.

PHP Write to File - fwrite()


The fwrite() function is used to write data to a file in PHP. It works with fopen() to create and modify files.



The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.


The example below writes a couple of names into a new file called "newfile.txt":

<?php
$file = fopen("newfile.txt", "w") or die("Unable to open file!");
$text = "Hello, this is a test file.\nWelcome to PHP file handling!";
fwrite($file, $text);
fclose($file);
?>

If we open the "newfile.txt" file it would look like this:

Hello, this is a test file.
Welcome to PHP file handling!

PHP Overwriting


Overwriting a file in PHP means replacing its existing content with new data.All the existing data will be ERASED and we start with an empty file.


In the example below we open our existing file "newfile.txt", and write some new data into it:

<?php
$file = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "This is new content.\nPrevious data is erased.";
fwrite($file, $txt);
fclose($file);
?>

If we now open the "newfile.txt" file, previous data have vanished, and only the data we just wrote is present:

This is new content.
Previous data is erased.

PHP Append Text


Appending text means adding new content to an existing file without deleting its previous content.


In PHP, you can append text using fopen() with "a" mode. The "a" mode appends text to the end of the file, while the "w" mode overrides (and erases) the old content of the file.


In the example below we open our existing file "newfile.txt", and append some text to it:

<?php
$file = fopen("newfile.txt", "a") or die("Unable to open file!");
$txt = "\nThis is a new line appended to the file.";
fwrite($file, $txt);
fclose($file);
?>

If we now open the "newfile.txt" file, we will see that "This is a new line appended to the file." is appended to the end of the file:

This is new content.
Previous data is erased.
This is a new line appended to the file.

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.