TUTORIAL: Basic PHP File I/O

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6250
  • Loc: Seattle, WA

Post 3+ Months Ago

Introduction


This tutorial will teach you how to read and write basic text files using PHP. Elementary knowledge of PHP, such as using variables, statements, and functions, is assumed.

Reading A File


To read the contents of a file into a variable, we use the file_get_contents() function. This function accepts a filename as an argument and reads the file into a string variable.

PHP Code: [ Select ]
$file_contents = file_get_contents('example.txt');
echo $file_contents;
  1. $file_contents = file_get_contents('example.txt');
  2. echo $file_contents;


By default, file_get_contents() will read in the entire file. If you only want to read in part of the file, you can specify offset and maxlen flags as parameters, which will allow you to specify where to start reading and how many characters to read in, respectively.

PHP Code: [ Select ]
$start = 10;  // start at the 10th character
$length = 50;  // only read in 50 characters max.
$file_contents = file_get_contents('example.txt', null, null, $start, $length);
  1. $start = 10;  // start at the 10th character
  2. $length = 50;  // only read in 50 characters max.
  3. $file_contents = file_get_contents('example.txt', null, null, $start, $length);


The filename can be a local file relative to the current script, such as in the examples above. Additionally, the file can be located on a remote server if your installation of PHP has been configured to allow it:

PHP Code: [ Select ]
$file_contents = file_get_contents('http://www.example.com');
echo $file_contents;
  1. $file_contents = file_get_contents('http://www.example.com');
  2. echo $file_contents;


How do you know if your file was read successfully? If file_get_contents() fails, it will return a boolean FALSE. Thus, we can check for file read failures:

PHP Code: [ Select ]
$file_contents = file_get_contents('example.txt');
if( $file_contents === false ) {
  // file wasn't read successfully, handle the error
}
else {
  echo $file_contents;
}
  1. $file_contents = file_get_contents('example.txt');
  2. if( $file_contents === false ) {
  3.   // file wasn't read successfully, handle the error
  4. }
  5. else {
  6.   echo $file_contents;
  7. }


Writing A File


Writing a file is just as straightforward as reading one. To write to a file, we use the function file_put_contents(). We pass two basic arguments to this function, a filename and the data we wish to write to it. Upon success, the function returns the number of bytes that were written.

If the file does not exist, it will be created.

PHP Code: [ Select ]
$data = "Hello World!";
$num_bytes = file_put_contents('example.txt', $data);
echo $num_bytes . ' bytes were written';
  1. $data = "Hello World!";
  2. $num_bytes = file_put_contents('example.txt', $data);
  3. echo $num_bytes . ' bytes were written';


By default, file_put_contents() will completely overwrite the file if it already exists. If we wish to append to an existing file, we must specify the FILE_APPEND flag:

PHP Code: [ Select ]
$data = "Hello World!";
$num_bytes = file_put_contents('example.txt', $data, FILE_APPEND);
echo $num_bytes . ' bytes were written';
  1. $data = "Hello World!";
  2. $num_bytes = file_put_contents('example.txt', $data, FILE_APPEND);
  3. echo $num_bytes . ' bytes were written';


We use the same mechanism to check for errors as we did when reading a file. The file_put_contents() will return boolean FALSE if it is unable to successfully write to the file:

PHP Code: [ Select ]
$data = "Hello World!";
$num_bytes = file_put_contents('example.txt', $data);
if( $num_bytes === false ) {
  // file wasn't written successfully
  echo 'Error writing file';
}
else {
  echo $num_bytes . ' bytes were written';
}
  1. $data = "Hello World!";
  2. $num_bytes = file_put_contents('example.txt', $data);
  3. if( $num_bytes === false ) {
  4.   // file wasn't written successfully
  5.   echo 'Error writing file';
  6. }
  7. else {
  8.   echo $num_bytes . ' bytes were written';
  9. }


Conclusion


You should now have a basic understanding of how to read and write simple text files in PHP.

If you have any questions regarding this tutorial, feel free to reply. Be sure to read the documentation for the two PHP functions used in this tutorial if you get stuck:

http://www.php.net/function.file-get-contents
http://www.php.net/function.file-put-contents
Moderator Remark: De-localized PHP manual links
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 1 post
  • Moderator: Tutorial Writers
  • Users browsing this forum: No registered users and 1 guest
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.