PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

fopen() and File Input/Output Functions

This section describes some file input and output built-in functions. fopen() opens a file for reading and writing. fclose() closes an opened file. fread() reads data from input files. fwrite() writes data to output files.

PHP offers a number of built-in functions to read and write files:

  • fopen($path,$mode) - This "file open" function opens a file at the specified path to read or write data and returns a file resource handle. The $mode argument specifies "r", "w", or "a" as the open mode for reading, writing or appending data to the file. On Windows system, The $mode argument takes another letter, "t" or "b", to control whether the file should be opened in text mode or binary mode. In text mode, the PHP engine will do automatic conversion between \n and \r\n. In binary mode, the PHP engine will not do any conversions. For example, fopen($path,"rb") opens a file for binary reading.
  • fclose($file) - This "file close" function closes the specified file resource handle.
  • fread($file,$length) - This "file read" function reads $length number of bytes from $file. and returns input bytes as a string.
  • fgets($file) - This "file get string" function reads one line from $file and returns it as a string.
  • feof($file) - This "file end of file" function returns true, if $file is pointing to the end of file currently.
  • fwrite($file,$data,$length) - This "file write" function writes $length number of bytes from $data to $file.
  • fscanf($file,$format,$var1,$var2,...) - This "file scan formatted" function parses input data into $var1, $var2,..., according to $format.
  • fprintf($file,$format,$var1,$var2,...) - This "file print formatted" function writes data from $var1, $var2,..., according to $format.
  • fflush($file) - This "file flush" function forces all buffered output to be written the actual $file.
  • ftruncate($file) - This "file truncate" function truncates $file to a given length.
  • ftell($file) - This "file tell" function returns the current position of the $file pointer in the actual file as an offset from the beginning of the file.
  • fseek($file,$offset) - This "file seek" function moves $file to the $offset position in the actual file.
  • rewind($file) - This "rewind" function $file position to beginning of the actual file.
  • flock($file,$locker) - This "file lock" function applies a locker on $file. 4 locker types are supported: LOCK_SH, LOCK_EX, LOCK_UN, and LOCK_NB.
  • fstat($file) - This "file statistics" function returns an array containing statistics information about $file. The returned array contains element keys of dev, ino, mode, nlink, uid, gid, rdev, size, atime, mtime, ctime, blksize, and blocks.
  • fgetc($file) - This "file get character" function reads 1 byte and return it as a string.

See the next section for example PHP scripts.

Last update: 2005.

Sections in This Chapter

opendir() and Directory Management Functions

file_exists() and File Testing Functions

FileExistsTest.php - File Testing Examples

fopen() and File Input/Output Functions

File_Input_Output_Test.php - File Input/Output Examples

readfile() and Special File Handling Functions

imagecreatetruecolor() and GD Imaging Library Functions

ShowPhoto.php - Simple Slid Show Script

Dr. Herong Yang, updated in 2009
fopen() and File Input/Output Functions