The following code shows an Example of Creating a File in PHP.
In order to create a file in PHP, we need to open it in write mode using the fopen() function. There are several modes available in PHP to open a file that we can use as a parameter in a call to fopen() function. For instance, the file mode “w” opens a file in write-only mode. When we use the “w” mode, the file is created if doesn’t exist already. In case, a file with the specified name is already there, the write-only mode “w” overwrites the existing contents of the file.
The following code creates a file with the name ‘phptutorial.txt’. After creating the file, we need to call the fwrite() function that writes the content available in the $data variable to the file using the file handle $atextfile returned by the fopen() function.
<?php // Creating a file $atextfile=fopen("phptuturial.txt", "w"); $data="PHP provides following functions for file handling\nfopen()\nfclose()\nfread()\nfwrite()"; fwrite($atextfile, $data); ?>
When you check the current folder, you will notice that a file with the name ‘phptutorial.txt’ is present there.
- Dot Net Framework
- Power Bi
- Scratch 3.0