--------------------------------------------------------------------------------
So much for reading files - how about actually writing one? In PHP,
this is somewhat more complex, involving as it does the use of file
"pointers" to write data to a file.
-------------------------------------------------
The function that does most of the work here is a little munchkin
called fopen(), which opens a pointer to the file. fopen()
requires two parameters - the name of the file, and the mode in
which the file is to be opened. This "mode" is one of the following:
-------------------------------------------------
1: Read-only mode, activated by the parameter "r". Here, the file
pointer is initialized at the first line of the file.
-------------------------------------------------
2: Write-only mode, activated by the parameter "w". Here, the file is
created anew and the pointer is initialized at the first line of
the file. If the file already exists, ALL ITS CONTENTS ARE LOST.
-------------------------------------------------
3: Read/write mode, activated by the parameter "r+". Here, the file
is opened for reading and writing, with the pointer initialized
at the first line of the file.
-------------------------------------------------
4: Append mode(write only), activated by the parameter "a".
In this case, the pointer is placed at the *end* of the file,
allowing you to append new content to the end of the file.
If the file doesn't exist, a new one is created.
-------------------------------------------------
5: Append mode(read/write only), activated by the parameter "a+".
Here too, the pointer is placed at the *end* of the file, allowing
you to append new content to the end of the file. If the file doesn't
exist, a new one is created. You can also read from the file.
-------------------------------------------------
Once you've got the file opened in the right mode, it's time to
actually write something to it with the fputs() function.
Our next example demonstrates this more clearly:
--------------------------------------------------------------------------------
The temporary file named 'myfile.txt' contains the following:
-------------------------
Warning: fopen(): open_basedir restriction in effect. File(/tmp/myfile.txt) is not within the allowed path(s): (/home/free-users) in /home/jail/home/free-users/p/primeooze/php/file/write_hard_code_to_file.php on line 81
Warning: fopen(/tmp/myfile.txt): failed to open stream: Operation not permitted in /home/jail/home/free-users/p/primeooze/php/file/write_hard_code_to_file.php on line 81
Warning: fputs(): supplied argument is not a valid stream resource in /home/jail/home/free-users/p/primeooze/php/file/write_hard_code_to_file.php on line 89
Warning: fclose(): supplied argument is not a valid stream resource in /home/jail/home/free-users/p/primeooze/php/file/write_hard_code_to_file.php on line 92
Warning: readfile(): open_basedir restriction in effect. File(/tmp/myfile.txt) is not within the allowed path(s): (/home/free-users) in /home/jail/home/free-users/p/primeooze/php/file/write_hard_code_to_file.php on line 96
Warning: readfile(/tmp/myfile.txt): failed to open stream: Operation not permitted in /home/jail/home/free-users/p/primeooze/php/file/write_hard_code_to_file.php on line 96
File is bytes in size.