Home Tutorial PHP Lesson 18 - Filesystem functions



MSN Random Online

Lesson 18 - Filesystem functions PDF Print E-mail
Written by Administrator   
Thursday, 15 February 2007 17:50

Lesson 18 - Filesystem functions

Filesystem Functions

The filesystem functions in PHP allow you to manipulate files.  You can open file, write to files, and updates files as you see fit with this set of functions.  The four main functions (at least in my opinion) for this group of functions are:

fopen - open a file to work with
fread - read the contents of a file
fwrite - write some content to a file
fclose - close a file you were working with

Using these functions is no different from using any other functions.  You simply have to read the documentation to learn about their arguments and what they return.  Please note the following URLs for references to the functions you will see in this tutorial.

http://www.php.net/manual/en/function.fopen.php
http://www.php.net/manual/en/function.fread.php
http://www.php.net/manual/en/function.fwrite.php
http://www.php.net/manual/en/function.fclose.php

If your scripts are running on a Unix operating system, you have to take extra care to see if you're allowed to read/write to the file you're trying to access.  When opening a file, you must always pass as arguments the location of the file your trying to open (either relative or absolute) and what you want to do with that file (read/write/append).

When opening files, these are the extra arguments you should be aware of (not all were included):
'r' - Open for reading only; place the file pointer at the beginning of the file.  If the file doesn't exist, you'll get an error.
'r+' - Open for reading and writing; place the file pointer at the beginning of the file.  If the file doesn't exist, you'll get an error.
'w' - Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.  Note that when writing, you lose all the other content in the file - you overwrite.
'w+' - 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 does not exist, attempt to create it.  Note that when writing, you lose all the other content in the file - you overwrite.
'a' - Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
'b' - binary.  This little mode can take up about 4 paragraphs of explanation as the PHP documentation clearly shows.  This mode must be the last letter if your mode consists of several of the above.

For example, to open a file just to read:

Code:
$filehandle = fopen('/path/to/file', 'r');

If you were on a windows platform and the file wasn't a standard text file, this would be a more suitable line:
Code:
$filehandle = fopen('/path/to/file', 'rb'); //b for binary

To write to a file or append to a file, these might be suitable lines of code:
Code:
$filehandle = fopen('/path/to/file', 'w'); //write to the file
$filehandle = fopen('/path/to/file', 'a'); //append to the file

fopen can also take a URL as an argument, but you can only read ('r' argument) from a URL, you cannot write/append to one.  After opening a file to read/write/append to, you can begin working with it using fread/fwrite.

PHP reads files in chunks (bytes).  When reading from a file, you must specify how many bytes you want to read.  You can read the entire file into memory, or you can read it in chunks.  The two examples given below show both methods (these two methods are very standard):

Read the entire file all at once
Code:
$filehandle = fopen('/path/to/file', 'rb');
$contents = fread($filehandle, filesize('/path/to/file'));

Read the file in portions
Code:
$filehandle = fopen('/path/to/file', 'r'); //open the file in read mode 
while (!feof($filehandle)) { //is there more to read?
  $contents .= fread($filehandle, 1024); //read the file in 1 meg portions
}
note: feof() tests to see if we've reached the end of the file yet

Remember that when you write to a file, you must have permission to do so.  Writing or appending to a file is as simple as:

Write:
Code:
$filehandle = fopen('/path/to/file', 'w'); //write
fwrite($filehandle, 'new contents of file'); //fputs() can be used as well

append:
Code:
$filehandle = fopen('/path/to/file', 'a'); //append
fwrite($filehandle, 'new contents added at the end of the file'); //fputs() can be used as well

If you wish to add new lines to a file, you can simply use "\n" to go to the next line.  Please remember that you'll need to either concatenate "\n" or include "\n" in double quotes with all your text so it gets interpolated into the newline character.

After you open a file with fopen, and you read/write contents using fread/fwrite, you should ALWAYS close the file.  This doesn't take any extra effort.  Remember to always include code like:

Code:
fclose($filehandle);

If you don't close your file (with the filehandle) after you finish working with it, you risk problems occurring.  If you don't close a file after writing to it, you risk the contents of the file being lost.  If you open a file, but don't close it, you also risk corrupting the file (which means you'll lose partial or all parts of the file).

Extra notes:
- If you try to open a file to write/append to, and it doesn't exist, PHP will try to create it
- Depending on which operating system you're working on, you might have to use "\r\n" (carriage return and newline) to go to the next line.  If you don't open your files in binary mode (mode b), this will transparently be done for you.  Please refer to the PHP documentation located at http://www.php.net/manual/en/function.fopen.php for a clear explanation of when and how to use binary mode.
- You can open webpage URLs to read, but not to write.  You can open FTP URLs to read and write to.  Make sure to use the proper formatting for FTPs: "ftp:// username:password @ url.com"
- You must have permission to read/write to a file.  PHP reads and writes to files as user 'nobody'.  This is different from root access to the files.  A file created by the root does not belong to 'nobody', but a file created by 'nobody' does belong to the root.
- If you don't have root access or someone hosts your website for you, chances are you don't have root access.  Creating files through ftp and files through a PHP scripts are two different things, and neither has privileges to touch the other.  If you create a file with PHP, you cannot delete it with FTP software and visa versa.  Simple rule to remember is: "if it made it, it must take care of it"
- you can use file() to read the contents of a file or URL into an array.  You can use file_get_contents() to read an entire file into a variable.  Both functions are binary safe now so they should handle line endings properly.

 

 
 

Top Members

No top members yet.