PHP Tutorials - Herong's Tutorial Examples
∟Functions to Manage Directories, Files and Images
∟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.
Table of Contents
About This Book
Introduction and Installation of PHP
PHP Script File Syntax
PHP Data Types and Data Literals
Variables, References, and Constants
Expressions, Operations and Type Conversions
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
Arrays - Ordered Maps
Interface with Operating System
Introduction of Class and Object
Integrating PHP with Apache Web Server
Retrieving Information from HTTP Requests
Creating and Managing Sessions in PHP Scripts
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
Managing File Upload
MySQL Server Connection and Access Functions
►Functions to Manage Directories, Files and Images
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
ShowPhoto.php - Simple Slid Show Script
SOAP Extension Function and Calling Web Services
SOAP Server Functions and Examples
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers
Parsing and Managing HTML Documents
Configuring and Sending Out Emails
Image and Picture Processing
Managing ZIP Archive Files
Managing PHP Engine and Modules on macOS
Managing PHP Engine and Modules on CentOS
Archived Tutorials
References
Full Version in PDF/EPUB