Loading video...
Introduction to File I/O
- File I/O typically involves three steps:
- Open a connection to a file
- Perform read/write operations on the file
- Close the connection to the file
- In order to read from or write to a file from PHP, the user account under which the PHP interpreter runs must have the appropriate read/write permissions on the file, depending on the desired operation.
fopen()is used to open a file. You must specify whether you wish to open the file for reading, writing, or both. This is done using the function's access mode parameter.fopen()returns a file handle, which represents a connection to an opened file. The file handle is a special PHP data type known as a resource.- File paths should always be specified using forward-slashes (/) as the path separator because it maximizes the portability of your code.
- Data in a file is accessed using a file pointer, which is a cursor used to keep track of the current character being accessed in a file.
- When opening a file with
fopen()using‘r’as the access mode, the file is opened for reading with the file pointer placed at the beginning of the file. fgets()reads the contents of the current line of a file and advances the file pointer to the beginning of the next line.feof()returnsTRUEif a file pointer’s current position is at the end of the file (EOF).fclose()is used to close the connection to an open file by passing it the file handle of the open file.trim()is used to strip any whitespace from the beginning or end of a string.- Additional Resources:
Introduction to File I/O
Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

































0 answers
Post by Rakesh Satapathy on November 3, 2012
Can we just not just use "fclose($filehandle);" instead of assigning it to a variable? i tested, but not sure it closes or not?