Changes

Jump to: navigation, search

PHP, Filesystems and File I/O

25 bytes added, 16:54, 13 May 2009
no edit summary
== Opening and Creating Files in PHP ==
Existing files are opened, and new files created using the PHP ''fopen()'' function. The ''fopen()'' function accepts two arguments and returns a ''file handle'' which is subsequently used for all future read and write interactions with that file. The first argument is the name (including the path) of the file to open. This path is relative to the server filesystem root, not your web server root. The second argument is an attribute indicating the mode in which you with to open the file (create, read only, write only etc). The following table lists the various file open attributes together with a brief description of each:
<google>ADSDAQBOX_FLOW</google>
Once a file has been opened it can be closed using the ''fclose()'' function. The ''fclose()'' function takes a single argument - the ''file handle'' returned by the ''fopen'' function when the file was first opened.
Given this information we can write a script to open a file. For the purposes of this example we will create a new file in the /tmp direcotry directory of our web server. We will open the file in ''w+'' mode so that it will be created if it does not already exist and provide both read and write access. We will then close the file using ''fclose()'':
<pre>
Having created and opened the file the next task is to write data to the file. We can do this using the PHP ''fwrite()'' and ''fputs()'' functions. These are essentially the same function so either can be used.
''fwrite()'' takes two arguments, the ''file handle'' returned form from the original ''fopen()'' callm call and the string to be written. We can, therefore, extend our example to write a string to our file:
<pre>
== Checking Whether a File Exists ==
The ''file_exists'' fuinction function can be used at any time to find if a file already exists in the filesystem. The function takes a single argument - the path to the file in question and returns a boolean ''true'' or ''false'' value depending on the existance existence of the file.
== Moving, Copying and Deleting Files with PHP ==
Files can be copied using the ''copy()'' function, renamed using the ''rename()'' function and deleted using the ''unlink()'' function. For example we can perform a number of tasks on our example file:
<pre>
Output buffering in PHP is a mechanism whereby content that would normally be sent directly to the output stream is initially held in a buffer until the buffer is flushed. This provides control over when output is presented to the user (perhaps in situations where there is a delay in gathering some data from a database for example).
Output Buffering is initaited initiated using the ''ob_start()'' function. ''ob_start()'' can be called with no arguments but does support three optional arguments:
* callback function - specifies a user defined function to be called before the buffer is flushed
?>
</pre>
 
<google>BUY_PHP_BOTTOM</google>

Navigation menu