PHP File Create
From w3cyberlearnings
Contents |
PHP Create File
Create a file in PHP is simily write to a file that is not already existed.
Syntax fopen
- w: write to a file
fopen(filename,'w');
Example 1
- If the file myfirstfile.txt is not existed yet, it will create a new file.
<?php $filename = "myfirstfile.txt"; $filehandle = fopen($filename, 'w') or die("can't open file"); fclose($filehandle); ?>
Example 2: create a list of a new file from an array
<?php $aa_file_name = array('apple', 'banana', 'orange'); foreach ($aa_file_name as $filename) { $filehandle = fopen($filename . '.txt', 'w') or die("can't open file"); } fclose($filehandle); ?>
Output: The following files are created
apple.txt banana.txt orange.txt