PHP File Handling

Using PHP file handling, We are going to learn how to open, create, read, write and delete the file.

PHP File Open

1. fopen() - PHP File Open

resource fopen (string $filename, string $mode [, bool $use_include_path = FALSE [,  resource $context ]] )  

fopen() binds a named resource, specified by filename, to a stream.

<?php 
 $handle = fopen("c:\folder\resource.txt", "r");
?>
A list of possible modes for fopen() using mode
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. In this mode, fseek() has no effect, writes are always appended.
'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. In this mode, fseek() only affects the reading position, writes are always appended.
'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
'x+' Create and open for reading and writing; otherwise it has the same behavior as 'x'.
'c' Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x'). The file pointer is positioned on the beginning of the file. This may be useful if it's desired to get an advisory lock (see flock()) before attempting to modify the file, as using 'w' could truncate the file before the lock was obtained (if truncation is desired, ftruncate() can be used after the lock is requested).
'c+' Open the file for reading and writing; otherwise it has the same behavior as 'c'.
'e' Set close-on-exec flag on the opened file descriptor. Only available in PHP compiled on POSIX.1-2008 conform systems.

PHP File Read

2. fread() - Binary-safe file read

string fread (resource $handle, int $length); 

fread() function is used to read the content of the file

<?php 
  $file_name = "c:\folder\resource.txt";
  $handle = fopen($file_name, "r");
  $file_contents = fread($handle, filesize($file_name));
  echo $file_contents;
  fclose($handle);
?>

PHP File Write

3. fwrite() - Binary-safe file write

int fwrite (resource $handle ,string $string [,  int $length ]); 

fwrite() writes the contents of string to the file stream pointed to by handle.

<?php 
  $file_name = "c:\folder\resource.txt";
  $handle = fopen($file_name, "w");
  fwrite($handle, 'Hello');
  fwrite($handle, 'World');
  fclose($handle);
?>

PHP Append File

4. fwrite() - used to writes and append the contents of string to the file stream pointed to by handle.

<?php 
  $file_name = "c:\folder\resource.txt";
  $handle = fopen($file_name, "a");
  fwrite($handle, 'Hello');
  fwrite($handle, 'World');
  fclose($handle);
?>

PHP Delete File

5. unlink() - Deletes a file

bool unlink (string $filename [, resource $context ])  

Deletes filename. Similar to the Unix C unlink() function. An E_WARNING level error will be generated on failure.

<?php 
  $file_name = "c:\folder\resource.txt";
  unlink($file_name);
?>

PHP Close File

6. fclose() - Closes an open file pointer

bool fclose (resource $handle)

The file pointed to by handle is closed.

<?php 
  $file_name = "c:\folder\resource.txt";
  $handle = fopen($file_name, "r");
  fclose($handle);
?>