Consider the following example for file creation through php
<?php // testfile.php $fh = fopen("testfile.txt", 'w') or die("Failed to create file"); $text = <<<_END Line 1 Line 2 Line 3 _END; fwrite($fh, $text) or die("Could not write to file"); fclose($fh); echo "File 'testfile.txt' written successfully"; ?>
When you run this in a browser, all being well, you will receive the message “File ‘testfile.txt’ written successfully”. If you receive an error message, your hard disk may be full or, more likely, you may not have permission to create or write to the file, in which case you should modify the attributes of the destination folder according to your operating system. Otherwise, the file testfile.txt should now be residing in the same folder in which you saved the testfile.php program. Try opening the file in a text or program editor—the contents will look like this:
This simple example shows the sequence that all file handling takes:
1. Always start by opening the file. This is done through a call to fopen.
2. Then you can call other functions; here we write to the file (fwrite), but you can also read from an existing file (fread or fgets) and do other things.
3. Finish by closing the file (fclose). Although the program does this for you when it ends, you should clean up yourself by closing the file when you’re finished.
Every open file requires a file resource so that PHP can access and manage it. The preceding example sets the variable $fh (which I chose to stand for file handle) to the value returned by the fopen function. Thereafter, each file handling function that accesses the opened file, such as fwrite or fclose, must be passed $fh as a parameter to identify the file being accessed. Don’t worry about the content of the $fh variable; it’s a number PHP uses to refer to internal information about the file—you just pass the variable to other functions.
Upon failure, FALSE will be returned by fopen. The previous example shows a simple way to capture and respond to the failure: it calls the die function to end the program and gives the user an error message. A web application would never abort in this crude way (you would create a web page with an error message instead), but this is fine for our testing purposes.
Notice the second parameter to the fopen call. It is simply the character w, which tells the function to open the file for writing. The function creates the file if it doesn’t already exist. Be careful when playing around with these functions: if the file already exists, the w mode parameter causes the fopen call to delete the old contents (even if you don’t write anything new!).
There are several different mode parameters that can be used here, as detailed in following table :
|‘r’||Read from file start||Open for reading only; place the file pointer at the beginning of the file. Return FALSE if the file doesn’t already exist.|
|‘r+’||Read from file start and allow
|Open for reading and writing; place the file pointer at the beginning of the file. Return FALSE if the file doesn’t already exist.|
|‘w’||Write from file start and
|Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file doesn’t exist, attempt to create it.|
|‘w+’||Write from file start, truncate
file and allow reading
|Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file doesn’t exist, attempt to create it.|
|‘a’||Append to file end||Open for writing only; place the file pointer at the end of the file. If the file doesn’t exist, attempt to create it.|
|‘a+’||Append to file end and allow
|Open for reading and writing; place the file pointer at the end of the file. If the file doesn’t exist, attempt to create it.|