|
| HOW TO CREATE A FILE IN PHP
PHP has a host of file functions enabling you to do almost anything with files.
For example, you can create a file from scratch with the following code:
<?
$fh=fopen("myfile.txt", "w") or die("can't open file");
fclose($fh);
?>
PHP will then create this file myfile.txt in the same directory as this php script used to create the file. An explanation of the code as follows:
We assign the $fh variable to the result of the fopen function. The fopen function takes two parameters, the first is the name of the file and the second is the opening mode. The w tells php to open and therefore create the file in write mode. If the file cannot be created, an error message can't open file will be displayed.
$fh=fopen("myfile.txt", "w") or die("can't open file");
After the line above, the file myfile.txt is created. We then tell PHP to close the file as follows:
fclose($fh);
|
|