Chapter 2 , introduced how to open, read, write, lock, close a file and other useful file functions.
Opening a File
Using fopen() to Open a File
fopen() definition:
resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )
fopen() examples:
$handle = fopen("/home/rasmus/file.txt", "r");
$fp = fopen("$_SERVER['DOCUMENT_ROOT']/../orders/orders.txt",'w');
Summary of File Modes for fopen()
| 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. |
| ‘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. |
| ‘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 PHP will generate a warning. If the file does not exist, attempt to create it. |
| ‘x+’ | Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE, and PHP will generate a warning. If the file does not exist, attempt to create it. |
| ‘b’ | Used in conjunction with one of the other modes. Opening file in binary mode, recommend. It is the default mode. |
| ‘t’ | Used in conjunction with one of the other modes. Opening file in text mode, not recommend. |
Other Points Requiring Attention
- You can open files via FTP, HTTP, and other protocols using fopen(). You can disable this capability by turning off the allow_url_fopen directive in the php.ini file.
- The scripts must has permission to access the file you are trying to use.
2. Reading and Writing Files
Reading from a File
string fgets ( resource $handle [, int $length ] )Gets a line from file pointer.
string fgetss ( resource $handle [, int $length [, string $allowable_tags ]] )Identical to fgets(), except that fgetss() attempts to strip any NUL bytes, HTML and PHP tags from the text it reads.
array fgetcsv ( resource $handle [, int $length [, string $delimiter = ',' [, string $enclosure = '"' [, string $escape='\\']]]] )Similar to fgets() except that fgetcsv() parses the line it reads for fields in CSV format and returns an array containing the fields read.
string fgetc ( resource $handle )Gets a character from the given file pointer.
string fread ( resource $handle , int $length )Reads up to length bytes from the file pointer referenced by handle.
int readfile ( string $filename [, bool $use_include_path = false [, resource $context ]] )Reads a file and writes it to the output buffer.
int fpassthru ( resource $handle )Reads to EOF on the given file pointer from the current position and writes the results to the output buffer.
array file ( string $filename [, int $flags = 0 [, resource $context ]] )Reads an entire file into an array, each element of the array corresponds to a line in the file, with the newline still attached.
string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen = -1 ]]]] )This function is similar to file(), except that file_get_contents() returns the file in a string , starting at the specified offset up to maxlen bytes.
Writing to a File
int fwrite ( resource $handle , string $string [, int $length ] )Writes the contents of string to the file stream pointed to by handle.
int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file. If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flags is set.
Navigating Inside a File
bool rewind ( resource $handle )Sets the file position indicator for handle to the beginning of the file stream.
int fseek ( resource $handle , int $offset [, int $whence = SEEK_SET ] )Sets the file position indicator for the file referenced by handle. The new position, measured in bytes from the beginning of the file, is obtained by adding offset to the position specified by whence.
int ftell ( resource $handle )Returns the position of the file pointer referenced by handle.
bool feof ( resource $handle )Tests for end-of-file on a file pointer.
3. Other Useful File Functions
bool fclose ( resource $handle )The file pointed to by handle is closed.
bool file_exists ( string $filename )Checks whether a file or directory exists.
int filesize ( string $filename )Gets the size for the given file.
bool unlink ( string $filename [, resource $context ] )Deletes filename. There is no function called delete.
bool flock ( resource $handle , int $operation [, int &$wouldblock ] )Locks or releases a file.
myvideo downloader
Very great post. I just stumbled upon your weblog and wanted to mention that I’ve really loved browsing your blog posts. In any case I’ll be subscribing on your rss feed and I’m hoping you write once more soon!