• Home

Logo

Navigation
  • Home
  • Articles
    • Content Writing
    • Design
    • General
    • Internet Marketing
    • Social Media
    • Tools and Tips
    • Usability
    • Web Hosting Articles
  • Tutorials
    • AJAX Tutorials
    • ASP Tutorials
    • C# Tutorials
    • CGI and Perl Tutorials
    • CSS Tutorials
    • Flash Tutorials
    • HTML Tutorials
    • Illustrator Tutorials
    • Java Tutorials
    • JavaScript Tutorials
    • Linux Tutorials
    • Miscellaneous Tutorials
    • MySQL Tutorials
    • Photoshop Tutorials
    • PHP Tutorials
    • Python Tutorials
    • Wireless Tutorials
    • WordPress Tutorials
    • XML Tutorials
  • Scripts
    • AJAX Scripts
    • ASP Scripts
    • ASP.NET Scripts
    • CGI & Perl Scripts
    • Flash Scripts
    • Java Scripts
    • JavaScript Scripts
    • PHP Scripts
    • Python Scripts
    • Remotely Hosted
    • Tools and Utilities
    • XML Scripts
  • Answers
  • Online Services
  • Tools

File I/O in PHP

By Brandon Cash | on Jun 23, 2005 | 0 Comment
PHP Tutorials
  • Tweet
  • Share
  • Tweet
  • Share

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:

Mode Description
r Open 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.
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.
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.
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.
x Create 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.

Share this story:
  • tweet

Author Description

Brandon is a young web developer who puts emphasis in new web standards
and all the best practices.  Working mostly in open source projects of
his own, he has come to a great understanding with PHP, and helps others
to reach an understanding of their own.

Over the years as a web developer and software tinkerer, he has learned
new ways to overcome previously impossible tasks and still learns with
every large project.  He considers the web a constantly changing
environment, and has adapted to fit it.

No Responses to “File I/O in PHP”

You must be logged in to post a comment.

Connect With Us

RSSSubscribe 1,241Followers 494Likes
  • Popular
  • Recent
  • Comments
  • Creating Energy Spheres in Photoshop

    Apr 15, 2008 - 96 Comments
  • Easy Screen Scraping in PHP with the Simple HTML DOM Library

    Aug 6, 2008 - 20 Comments
  • Calculating date difference more precisely in PHP

    Mar 7, 2008 - 13 Comments
  • When Does Hosting Your Website in the Cloud Make Sense?

    Oct 8, 2010 - 2 Comments
  • Fun with the Microsoft Managed Extensibility Framework Part 2

    Oct 6, 2010 - 0 Comment
  • Fun with the Microsoft Managed Extensibility Framework Part 1

    Sep 22, 2010 - 0 Comment
  • Website Management on the go with the iPad

    I appreciated your post, but I was looking for something I didn't...
    November 24, 2012 - drmoderator
  • Creating Energy Spheres in Photoshop

    I'm a little stuck down here especially at the step of creating the...
    November 23, 2012 - sarah
  • Running background processes in PHP

    Can you give an example? As see it, you can use this only when you...
    November 16, 2012 - Shaked Klein Orbach
Developer Resources
  • Tutorial Directory
  • Learn HTML
  • Learn PHP
  • Learn CSS
  • Learn AJAX
  • Learn JavaScript
  • Learn Pear
  • White Papers
  • Resources
    • NetVisits Web Directory
    • Realtor Pixels
    • Answers On The Run
    • Ask A Geek
  • Recent Posts

    • When Does Hosting Your Website in the Cloud Make Sense?
    • Fun with the Microsoft Managed Extensibility Framework Part 2
    • Fun with the Microsoft Managed Extensibility Framework Part 1
    • Website Management on the go with the iPad
    • Code Contracts in C# 4.0 – Part 1

    Calendar

    May 2013
    M T W T F S S
    « Oct    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  

    Recent Comments

    • drmoderator on Website Management on the go with the iPad
    • sarah on Creating Energy Spheres in Photoshop
    • Shaked Klein Orbach on Running background processes in PHP
    • Thomas Cuvillier on How To Upload Files Using PHP
    • rizal aditya on Extracting text from Word Documents via PHP and COM
    • Home
    © 2003 - 2013 DeveloperTutorials.com. All Rights Reserved. Privacy Policy.