PHP File I/O
You can use a number of standard functions to perform file I/O including:
- fopen - opens file
- fgets - gets one line at a time
- fputs
- fread - reads set amount of bytes
- fwrite
- fscanf
- fseek
- file_get_conents and file_put_contents
- readFile
- file_exists
- fclose
Check out also the series of functions like is_readable and is_writable that checks permissions. Also, fflush is a useful command to flush file I/O commands. Check out a complete list at php.net under Filesystem functions.
Here is some example PHP code to open a file and read one line at a time from the file and finally closes the pointer to the file.
$file_handle = fopen("myfile", "r"); while (!feof($file_handle)) { $line = fgets($file_handle); echo $line; } fclose($file_handle);Try it out now
The next set of code reads the entire file$fh = fopen("readfile.txt", "rb"); $data = fread($fh, filesize("myfile")); fclose($fh);