Does anyone know a way to create a txt file using php?
I want my webpage in a specific function to create a txt file (which name will be taken from a variable, for example $name). Then i will open the file using fopen function, write to it using fwrite and close it using fclose, but i don't know how to create the file.
create a txt file with php
the fopen() command should create the file.
I just used this code from http://www.tizag.com/phpT/filecreate.php to make a simple file on my server and it created the file:
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
The "mode" flag in the fopen() call defines what happens to the file at the path you specify.
r and r+ :: Read the file only, no writing.
w and w+ :: Write to a file, if it doesn't exist, create it. If it has data, remove all of it.
a and a+ :: Write to a file, if it doesn't exist, create it. If it has data, go to the very end of the file and start writing.
x and x+ :: Create and open the file for writing only. If it already exists, the fopen call will fail.
All of this can be found on the [man]fopen/man manual page.