Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

File I/O in PHP

By Brandon Cash
2005-06-23


File I/O in PHP

File access in PHP is much simpler than people seem to think. It is quite a bit different from a database, yet remarkably similar. As I am hoping to show you through this tutorial, file input and output is a synch once you get the hang of it. Plus, it's almost identical in every language--PHP, Perl, C++, and even Visual basic. Once you have the concept down, it will transcend from language to language (of course, syntax will be slightly different), which is a major plus over using some language-specific concepts.

PHP uses what is called a file pointer to reference open files. You can create a new file pointer by using fopen(). This function takes, at minimum, two parameters: filename is the file that you wish to open, and mode is the mode you wish to open it with. While filename is fairly straight forward, mode can be a bit more difficult to grasp, and is definately harder to remember. There are eight different modes that you can use to open a file:

ModeDescription
rOpen for reading only; place the file pointer at the beginning of the file.
r+Open for reading and writing; place the file pointer at the beginning of the file.
wOpen 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.
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.
aOpen 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.
xCreate and open for writing only; place the file pointer at the beginning of the file.
x+Create and open for reading and writing; place the file pointer at the beginning of the file.

Here are several examples to open a new file pointer:

  $fp = fopen('file.txt', 'r'); // Open file.txt for reading from beginning
  $fp = fopen('/usr/local/data/file.txt', 'w+'); // Open a file in another directory for writing and clear its contents
  $fp = fopen('http://www.yahoo.com/', 'r'); // Open a webpage for reading. Only works if HTTP wrappers are enabled.

The method with which you opened the file is very important. If you plan on reading, you need to open with reading support, and the same goes for writing. The functions fgets() and fread() are the two most used ways of reading from a file, so I will explain them in depth. Fread() is the easiest way to read a file, if you know where to start and where to stop. It takes two parameters, handle--our file pointer--and length--the number of bytes to read. You should note that this function starts at the current location of the file pointer. As you keep reading, it will push the file pointer further through the file. If you want to read all of the file, the filesize() function will be of use.

An alternate reading function is fgets(). This is a bit different in how it reads, however. It reads a single line a time. You can specify length as well. The function terminates reading on three possibilities: end of line (EOL) is found (this is the \n character, ASCII code 10), the length has been met, or end of file (EOF) has been met. Default length is 1024 bytes.
Here are some examples of reading from a file:

  $read = fread($fp, 1024); // Read 1024 bytes from the file pointer
  $read = fgets($fp, 2048); // Read up to 2048 bytes, unless EOL or EOF is found.

Of course, in contrast to reading, there is also writing. For writing, there is only one function that you need to use, fwrite(). It takes two parameters by default: handle--the file pointer--and string--what to write. You can also specify length, which limits how much you are writing. Examples of writing to various text files:

  fwrite($fp, 'This is some text'); // Writes to the file pointer

  if (!fwrite($fp, 'This is some text')) {} // This conditional checks to see if we could not write to the file pointer

And, since you have opened a file pointer, you must close it. This is achieved by using fclose(). It only takes the file pointer as its parameter.

Hopefully this tutorial will help get you started with file input and output. Not every function was covered (which is why you should always check the manual!), but it should be a good guide.



Tutorial Pages:
» File I/O in PHP


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1

Ask A Question
characters left.