filename:
If filename begins with "http://" (not case sensitive), an HTTP
connection is opened to the specified server, the page is requested
using the HTTP GET method.
If filename begins with "ftp://" (not case sensitive), an ftp
connection to the specified server is opened and a pointer to the
requested file is returned. If the server does not support passive
mode ftp, this will fail.
If filename is one of "php://stdin", "php://stdout", or
"php://stderr", the corresponding stdio stream will be opened.
If filename begins with anything else, the file will be opened from
the filesystem.
mode may be any of the following (read, write or append):
'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.
'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.
If the final optional parameter use_include_path is set to true (or 1) an attempt will be made to find the file in the directories specified by PHP's include path.
Examples:
Close: Although all file references are automatically closed upon the termination of the PHP script which opened them, it is good programming practice to close the files manually with fclose():
Example (open and close a file test.txt in /tmp/):
Functions Related File Opening:
int fflush (int fp): Forces a write of all buffered output
int popen (string command, string mode): Opens a pipe to a process executed by forking the command. Example:
int fsockopen (string [udp://]hostname, int port [, int errno [, string errstr [, double timeout]]])
Open Internet or Unix domain socket connection.
bool flock (int fp, int operation [, int wouldblock])
Locks a file.
Example:
The fread functions allows (binary-safe) file read of blocks of bytes:
string fgetc (int fp):
Gets character from file pointer.
It returns a string containing a single character
and returns FALSE on EOF.
mixed fscanf (int fp, string format [, string var1...]): Is a C-style function, it parses input from a file according to a format.
The complement of fgets() and fread() is fwrite() and fputs() which is an alias to fwrite():
The fseek function sets the file position indicator for the file referenced by fp:
SEEK_SET - Set position equal to offset bytes (default),
(measure from the beginning of the file).
SEEK_CUR - Set position to current location plus offset.
SEEK_END - Set position to end-of-file plus offset.
Upon success, returns 0; otherwise, returns -1.
int ftell (int fp): Returns the position of the file pointer referenced by fp; i.e., its offset from the beginning of the file.
int rewind (int fp): Sets the file position indicator for fp to the beginning of the file.
int feof (int fp): Returns TRUE if the file pointer is at EOF or an error occurs; otherwise returns FALSE.
Example:
stringfile_get_contents
(string filename [, int use_include_path [, resource context]]):
Reads entire file into a string.
It is identical to file(), except that file_get_contents() returns the
file in a string.
Note: This function is binary-safe.
(See example in section "Binary Data").
int
file_put_contents
( string filename, string data [, int flags [, resource context]])
:
Writes a string to a file.
flags can take FILE_USE_INCLUDE_PATH and/or FILE_APPEND.
Note: This function is binary-safe.
(See example in section "Binary Data").
string pack ( string format [, mixed args]):
Packs data into binary string.
It packs the given arguments into binary string according to format
and returns it.
The idea to this function was taken from Perl and all formatting codes
work the same (some are missing such as Perl's "u").
Currently implemented are:
a NUL-padded string A SPACE-padded string h Hex string, low nibble first H Hex string, high nibble first c signed char C unsigned char s signed short (always 16 bit, machine byte order) S unsigned short (always 16 bit, machine byte order) n unsigned short (always 16 bit, big endian byte order) v unsigned short (always 16 bit, little endian byte order) i signed integer (machine dependent size and byte order) I unsigned integer (machine dependent size and byte order) l signed long (always 32 bit, machine byte order) L unsigned long (always 32 bit, machine byte order) N unsigned long (always 32 bit, big endian byte order) V unsigned long (always 32 bit, little endian byte order) f float (machine dependent size and representation) d double (machine dependent size and representation) x NUL byte X Back up one byte @ NUL-fill to absolute positionThe repeater argument can be either an integer value or * for repeating to the end of the input data. For a, A, h, H the repeat count specifies how many characters of one data argument are taken, for @ it is the absolute position where to put the next data, for everything else the repeat count specifies how many data arguments are consumed and packed into the resulting binary string.
Example:
array unpack ( string format, string data):
Unpacks from binary string into array according to format. Returns
array containing unpacked elements of binary string.
Example:
Comments and Notes on Binary Data: An alternative to binary data storage are "bzip2ed" ASCII-data-files.
PHP offers a lot of support for transfering binary images.
Note:
On systems running PHP which differentiate between binary and text
files (such as Windows), you'll need to open the file in binary mode
by including "b" along with any other modes that you may require
(e.g.,"rb").
Note:
If you have "magic quotes" enabled, fread() may not function as expected
(especially when dealing with a null character). PHP will
automatically convert the "null" character to its escaped version \0
in the resulting input. Toggling "magic quotes" can be done by using
the set_magic_quotes_runtime() and get_magic_quotes_runtime()
functions.
int filesize (string filename): Returns the size of the file (will not work on remote files).
Other file-info functions (see manual for more info):
stat : Gives information about a file realpath : Returns canonicalized absolute pathname pathinfo : Returns information about a file path linkinfo : Gets information about a link readlink : Returns the target of a symbolic link filetype : Gets file type fileatime : Gets last access time of file filectime : Gets inode change time of file fileinode : Gets file inode filegroup : Gets file group filemtime : Gets file modification time fileowner : Gets file owner fileperms : Gets file permissions is_dir : Tells if filename is a directory is_executable : Tells if filename is executable is_file : Tells if filename is a regular file is_link : Tells if filename is a symbolic link is_readable : Tells if filename is readable is_writable : Tells if filename is writable is_writeable : Tells if filename is writable diskfreespace : Returns available space in directory disk_total_space : Returns the total size of a directory
chmod ("/somedir/somefile", 0755);
int chown (string filename, mixed user): Changes the file owner.
int chgrp (string filename, mixed group): Changes the file group.
int rename (string oldname, string newname): Attempts to rename oldname to newname and returns TRUE on success.
int mkdir (string pathname, int mode): Attempts to create the directory specified by pathname.
int unlink (string filename): Deletes filename. Similar to the Unix C unlink() function. Returns 0 or FALSE on an error.
int rmdir ( string dirname): Attempts to remove the directory named by dirname. The directory must be empty.
Other filesystem functions (see manual for more info):
link : Create a hard link symlink : Creates a symbolic link tempnam : Creates unique file name tmpfile : Creates a temporary file touch : Sets modification time of file
dirname(), is_dir(), mkdir(), rmdir() : see the Filesystem section. getcwd : gets the current working directory chdir : Change directory chroot : Change the root directory opendir : open directory handle closedir : close directory handle readdir : read entry from directory handle rewinddir : rewind directory handle dir : directory class
There exists also a directory class called dir, a pseudo-object oriented mechanism for reading a directory.
string dirname (string path): Returns directory name component of path.