PHP Set 6 Flashcards
How can you read a file in PHP and what is the syntax?
readfile() function allows you to read a file and write it to the output buffer
Syntax:
readfile(string $filename, bool $use_include_path=false, resource $context): int | false
What does the readile function return?
Returns the number of bytes read from the file on success or false on failure
How do you open a file and what is the syntax?
Using the fopen() function
Syntax:
fopen(string $filename, string $mode, bool $use_include_path=false, resource $context): resource | false
What does the fopen function return?
Returns a file pointer resource on success or false on failure
What are the possible modes you can select when opening a file?
- ‘r’ is to open for reading only
- ‘r+’ is to open for reading and writing
- ‘w’ is to open for writing only
- ‘w+’ is to open for reading and writing
- ‘a’ is to open for writing only
- ‘a+’ is to open for reading and writing
- ‘x’ is to create and open for writing only
- ‘x+’ is to create and open for reading and writing
- ‘c’ is to open the file for writing only
- ‘c+’ is to open the file for reading and writing
What is the difference between ‘w’, ‘a’, ‘x’, and ‘c’ file open modes since they are all used for writing only?
w places the file pointer at the beginning of the file and truncates the file to zero length before writing. If the file does not exist it will try to create it
a places the file pointer at the end of the file. fseek function has not effect as writes are always appended. If the file does not exist it will try to create it
x places the file pointer at the beginning of the file. If the file already exists the open call will fail and an E_WARNING error will be generated
c positions the file pointer at the beginning of the file. It will not truncate it or throw an error if the file already exists.
What function is used to read from an open file and what is the syntax?
Use the fread() function to read up to a specified number of bytes from the stream created by fopen()
Syntax:
fread(resource $stream, int $length): string | false
What does the fread function return?
Returns the read data as a string on success or false on failure
How can you get the size of a file in bytes?
Using the filesize(string $filename) function
How do you close a file?
Using the fclose(resource $stream) function
What does the fgets function do and what is its syntax?
It is used to read a single line from an opened file
Syntax:
fgetsIresource $handle, int $length=?): string | false
What does the fgets function return?
Returns a string of up to length-1 bytes read from the file on success or false on failure
What is the difference between fread and fgets?
fread is a binary-safe file read and reads up to the number of bytes that must be specified.
fgets gets a line from a file pointer, it reads a line and stops when it encounters a newline if a length is not provided
What does the fclose function return?
Returns true on success or false on failure
What does the feof function do and what is its syntax?
Checks if the end of file has been reached
Syntax:
feof(resource $stream): bool