Core PHP

File Stream Functions

File Stream Functions
fopen()
resource fopen(string $sFilename, string $sMode, bool $bUseIncludePath = false, resource $qContext = NULL)
Create a file handle to the file stream resource. If successful, the handle is returned. Otherwise, the functions returns FALSE.
Argument Description
string $sFilename The name of the file.
string $sMode The opening mode: reading, writing, etc.
  • 'r' - Open for reading, File pointer at the beginning
  • 'r+' - Open for both, File pointer at the beginning
  • 'w' - Open for writing, File pointer at the beginning, Clear the file data, Create a new file as needed
  • 'w+' - Open for both, File pointer at the beginning, Clear the file data, Create a new file as needed
  • 'a' - Open for writing; File pointer at the end, Create a new file as needed, fseek() is disabled, Writes are always appended
  • 'a+' - Open for both; File pointer at the end, Create a new file as needed, fseek() is for reading, Writes are always appended
  • 'x' - Create and open for writing, File pointer at the beginning, Fails if the file exists
  • 'x+' - Create and open for reading and writing; otherwise it has the same behavior as 'x'.
  • 'c' - Open for writing, Create a new file as needed. File is not truncated (cleared out), File pointer at the beginning
  • 'c+' - Open for both, 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.
bool $bUseIncludePath = false Specifies whether we should use an include path. By default this is false.
resource $qContext A context. By default, this is NULL.
tmpfile()
resource tmpfile(void)
Create a temporary file with a unique name in both read and write mode. If successful, the handle to the file is returned. Otherwise, the functions returns FALSE. The file is automatically deleted when it is closed or when there are no references to the file handle, including when the program ends.
fclose()
bool fclose(resource $qFileHandle)
Argument Description
resource $qFileHandle A handle that references the file to be closed.
This closes open file that the file handle references. The file handle must reference a file that was successfully opened with fopen() or fsockopen(). The function returns TRUE, if successful. Otherwise, it returns FALSE.
fread()
string fread(resource $qFileHandle, int $iLength)
This reads up to $iLength bytes from the file referenced by $qFileHandle and puts them into the string that is returned. Reading stops when anyone of the following conditions occurs: The end of the file has been reached. $iLength bytes have been read and written into the string. For network streams, a packet becomes available or the socket timeout occurs. If the stream is read buffered and it does not represent a plain file, at most one read of up to a number of bytes equal to the chunk size (usually 8192) is made; depending on the previously buffered data, the size of the returned data may be larger than the chunk size.
Argument Description
resource $qFileHandle A handle that references the file to be read.
int $iLength The number of bytes to be read.
fgetc()
string fgetc(resource $qFileHandle)
This function returns the character, as a string, at the file pointer and advances the file pointer. It returns FALSE if the file pointer is at the end of the file.
Argument Description
resource $qFileHandle A handle that references the file from which the character is to be read.
fgetcsv()
array fgetcsv(resource $qFileHandle, int $iLength = 0, string $sDelimiter = ",", string $qEnclosure = '"', string $qEscape = "\")
This function reads up to $iLength characters, if it is not zero, of each set of delimited characters into an entry of the array that is return. It returns an array of strings from the file, separated by commas. It returns FALSE if the handle is invalid and FALSE if the file pointer is at the end of the file or other error occurs.
Argument Description
resource $qFileHandle This is a handle that references the file from which the line of text is to be read.
int $iLength This is the number of characters to be read, if not zero.
string $sDelimiter This is the delimeter character that is used to split the line into strings. Only one character is allowed.
string $qEnclosure This is the enclosure character. Only one character is allowed.
string $qEscape This is the escape character. Only one character is allowed.
fgets()
string fgets(resource $qFileHandle, int $iLength = 0)
This function returns a line of characters up to ($iLength - 1) characters as a string at the file pointer and advances the file pointer. It returns FALSE if the file pointer is at the end of the file or an error occurred.
Argument Description
resource $qFileHandle This is a handle that references the file from which the line is to be read.
int $iLength This is the maximum number of characters that are to be read. If this is zero, then no maximum is specified.
fgetss()
string fgetss(resource $qFileHandle, int $iLength = 0, string $sAllowableTags = NULL)
This function returns a line of characters up to ($iLength - 1) characters as a string with all NULL bytes, HTML tags, and PHP tags stripped out of it at the file pointer and advances the file pointer. It returns FALSE if the file pointer is at the end of the file or an error occurred.
Argument Description
resource $qFileHandle This is a handle that references the file from which the line is to be read.
int $iLength This is the maximum number of characters that are to be read. If this is zero, then no maximum is specified.
string $sAllowableTags This is the the set of tags that will not be stripped out. For example, we could have $sAllowableTags = array("html", "body", "b", "br", "em", "hr", "i", "li", "ol", "p", "s", "span", "table", "tr", "td", "u", "ul");
fwrite()
int fwrite( resource $qFileHandle, string $sToWrite, int $iLength = 0)
This function writes a string of characters up to ($iLength - 1) characters at the file pointer and advances the file pointer. It returns the number of bytes written or FALSE if an error occurred.
Argument Description
resource $qFileHandle This is a handle that references the file to which to write the string.
string $sToWrite This is the string that is to be written to the file.
int $iLength This is the maximum length in bytes that is to be written in bytes. If it is zero, there is no maximum.
fputs()
The fputs() function is exactly the same as fwrite().
fseek()
int fseek( resource $qFileHandle, int $iOffset, int $iWhence = SEEK_SET)
This function sets file pointer for the file referenced by $qFileHandle. The new position is the $iOffset, specified by $iWhence. It returns 0 if it is successful. Otherwise, it returns -1.
Argument Description
resource $qFileHandle This is a handle that references the file to be accessed.
int $iOffset This specifies the new position.
int $iWhence This value designates the positioning mode:
  • SEEK_SET - Set position equal to offset bytes.
  • SEEK_CUR - Set position to current location plus offset.
  • SEEK_END - Set position to end-of-file plus offset.
ftell()
int ftell(resource $qFileHandle)
This function returns the position of the file pointer for the file referenced by $qFileHandle. If an error occurs, it returns FALSE.
Argument Description
resource $qFileHandle This is a handle that references the file that is to be accessed.
fstat()
array fstat(resource $qFileHandle)
This function returns statistics for the file referenced by $qFileHandle. If it fails, it returns FALSE. The statistics array is accessed by index or string as follows:
  • 0 or 'dev' - device number
  • 1 or 'ino' - inode number, always zero on Windows
  • 2 or 'mode' - inode protection mode
  • 3 or 'nlink' - number of links
  • 4 or 'uid' - userid of owner, always zero on Windows
  • 5 or 'gid' - groupid of owner, always zero on Windows
  • 6 or 'rdev' - device type, if inode device
  • 7 or 'size' - size in bytes
  • 8 or 'atime' - time of last access (Unix timestamp)
  • 9 or 'mtime' - time of last modification (Unix timestamp)
  • 10 or 'ctime' - time of last inode change (Unix timestamp)
  • 11 or 'blksize' - blocksize of filesystem IO, always -1 on Windows
  • 12 or 'blocks' - number of 512-byte blocks allocated, always -1 on Windows
Argument Description
resource $qFileHandle This is a handle that references the file that is to be accessed.
feof()
bool feof(resource $qFileHandle)
This function returns TRUE if the file pointer referenced by $qFileHandle is at the end of the file. Otherwise, it returns FALSE.
Argument Description
resource $qFileHandle A handle that references the file to be checked.
fflush()
bool fflush(resource $qFileHandle)
This function flushes the values that are still in the buffer. It returns TRUE, if it is successful. Otherwise, it returns FALSE.
Argument Description
resource $qFileHandle A handle that references the file to be flushed.
flock()
bool flock(resource $qFileHandle, int $iOperation, int &$irWouldBlock = 1)
This function locks the file that $qFileHandle references in the manner specified by $iOperation. It returns TRUE, if it is successful. Otherwise, it returns FALSE.
Argument Description
resource $qFileHandle This is a handle that references the file to be locked.
int $iOperation This is the locking specification, which is given by one of the following:
  • LOCK_SH to acquire a shared lock for a reader.
  • LOCK_EX to acquire an exclusive lock for a writer.
  • LOCK_UN to release either of these locks.
Additionally, we can bitwise or LOCK_NB with either of the lock modes to prevent blocking with the lock.
int &$irWouldBlock This is be set to 1 if the lock would block--the EWOULDBLOCK errno condition.
fpassthru()
int fpassthru(resource $qFileHandle)
This function reads the bytes, from the file pointer that the file $qFileHandle references to the end of the file, into the output buffer. It returns the number of bytes read, if it is successful. Otherwise, it returns FALSE.
Argument Description
resource $qFileHandle This is a handle that references the file.
ftruncate()
bool ftruncate(resource $qFileHandle, int $iSize)
This function truncates the file that $qFileHandle references to the number of bytes indicated by int $iSize. If $iSize is larger than the file size, the file is padded with null bytes. It returns TRUE, if it is successful. Otherwise, it returns FALSE.
Argument Description
resource $qFileHandle This is a handle that references the file.
int $iSize This is the size of the resulting file.
stream_set_write_buffer()
int stream_set_write_buffer(resource $qFileHandle, int $iBufferSize)
This function sets the write buffer size for the file that $qFileHandle references. It returns 0, if it is successful. Otherwise, it returns a different value.
Argument Description
resource $qFileHandle This is a handle that references the file.
int $iBufferSize This is the size of the write buffer for the file. If the buffer is 0 bytes then write operations are unbuffered. This ensures that all writes with fwrite() are completed before other processes are allowed to write to that output stream.
set_file_buffer()
The set_file_buffer() function is exactly the same as stream_set_write_buffer().
rewind()
bool rewind(resource $qFileHandle)
This function moves the file pointer the file that $qFileHandle references to the beginning of the file. It returns TRUE, if it is successful. Otherwise, it returns FALSE.
Argument Description
resource $qFileHandle This is a handle that references the file.
fprintf()
int fprintf(resource $qFileHandle, string $sFormatted, ...)
This function writes the string $sFormatted to the file that $qFileHandle at the file pointer. It returns TRUE, if it is successful. Otherwise, it returns FALSE.
Argument Description
resource $qFileHandle This is a handle that references the file.
string $sFormatted This is a formatted string, with possible values specified by the remaining arguments.
 
 

© 2007–2024 XoaX.net LLC. All rights reserved.