The function fopen() takes in two arguments, the filename and the mode to either open or create a file.
- $filename - the name of the file. This may also include the absolute path where you want to create the file. Example, "/www/myapp/myfile.txt".
- $mode - mode is used to specify how you want to create the file. For example, you can set the mode to create for read only, or create a file for read and write.
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
- $ourFileName = "testFile.txt";
The name of our file is "testFile.txt" and it is stored into a String variable $ourFileName.
- $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
We use the function fopen and give it two arguments: our file name and we inform PHP that we want to write by passing the character "w".
- fclose($ourFileHandle);
We close the file that was opened. fclose takes the file handle that is to be closed.