PHP File Handling

The PHP allows to work with files stored on the web server. With php File system functions we can create, access, read, write and manipulate files on the web server. The fuctions mainly used in file handling is :

fopen()
fclose()
fread()
fwrite()


Opening a file : fopen()

The fopen() function is used to open a file. The basic syntax of fopen() is :
 fopen(filename, mode)
Where the first parameter filename specifies the name of the file we want to open, and the second parameter mode specifies in which mode the file should be open. For example :
 $myfile = fopen("myfile.txt", "r")
At above statement the file myfile.txt opens in mode "r", means read only and the function will return the pointer to the file to be opened. The modes available in PHP are as follows :

Modes Description
r Open a file for read only. The file pointer starts at the beginning of the file. Returns false if the file doesn’t exist.
r+ Open a file for read/write. The file pointer starts at the beginning of the file. Returns false if the file doesn’t exist.
w Open a file for write only. If the file already exists then erase its contents otherwise create new file if it doesn't exist. File pointer starts at the beginning of the file.
w+ Open a file for read/write. If the file already exists then erase its contents otherwise create new file if it doesn't exist. File pointer starts at the beginning of the file.
a Open a file for write only. The existing data in file is preserved. Creates a new file if the file doesn't exist. File pointer starts at the end of the file.
a+ Open a file for read/write. The existing data in file is preserved. Creates a new file if the file doesn't exist. File pointer starts at the end of the file.
x Creates a new file for write only. Returns FALSE and an error if file already exists.
x+ Creates a new file for read/write. Returns FALSE and an error if file already exists.


Closing a file : fclose()

The fclose() function is used to close an open file in php. The fclose() function requires a file pointer as its argument and then returns true when the closure succeeds or false if it fails. lets see an example of it :
<?php

  $file = "file.txt"
  $myfile = fopen($file, "r");
  // some code to be executed....
  fclose($myfile);

?>

Reading from a file : fread()

The fread() function is used to read a specified number of characters from a file. The basic syntax of fread() function is :
 fread(file_pointer, file_size)
Where the first argument is the file pointer or file handle, and the second argument file_size is nothing but the size or length of file in bytes. Now lets see an example. For example we created a file "file.txt", with the size 25 bytes.
"This is Test String"
PHP code to read the above file :
<?php

  $file = "file.txt";

  $myfile = fopen($file, "r");        // open the file
  $file_content = fread($myfile, 25); // reading the content of file
  fclose($myfile);                    // closing the file
  echo $file_content . "<br/>";

?>
Output :

This is Test String

We can also use the filesize() function to get the size/length of file which we want to read. The syntax of filesize() is :
 filesize(file_name);
Example :
<?php

  $file = "file.txt";

  $myfile = fopen($file, "r");       
  $size = filesize($file);     
  echo "File Size : $size Bytes <br/><br/>"; 
  $file_content = fread($myfile, $size); 
  fclose($myfile);                      
  echo $file_content . "<br/>";

?>
Output :

File Size : 20 Bytes

This is Test String

There's also another function called readfile(), which reads a file and writes the data to the output buffer. This readfile() function allows us to read the contents of a file without needing to open it. Example :
<?php

  $file = "file.txt";
  readfile($file);

?>
Output :

This is Test String

It also returns the size of file.


Writing to a File : fwrite()

The fwrite() function issued to write data into a file. The basic syntax of fwrite() is :
 fwrite(file_pointer, string)
Where the first argument is the file pointer or file handle, and the second argument string is nothing
but the string we want to write into the file. Lets see the below example :
 <?php

  $file = "file.txt";

  // string to write
  $str = "This is the String to write into the file.";

  // opening the file
  $myfile = fopen($file, "w");

  // write data into the file
  fwrite($myfile, $str);

  // closing the file
  fclose($myfile);

?>

Next Topic :