Chapter 2 Storing and Retrieving Data Flashcards
Define and Show: fopen()
Opens a file.
$fp = fopen(“$DOCUMENT_ROOT/bobs/orders.txt, ‘ab’);
fopen file mode: r
Read. Open the file for reading, beginning from the star of the file.
fopen file mode: r+
Read. Open the file for reading and writing, beginning from the start of the file.
fopen file mode: w
Write. Open the file for writing, beginning from the start of the file. If the file exists, delete the existing contents. If it does not exist, try to create it.
fopen file mode: w+
Write. Open the file for writing and reading, beginning from the start of the file. If the file already exists, delete the existing contents. If it does not exist, try to create it.
fopen file mode: x
Cautious write. Open the file for writing, beginning from the start of the file. If the file already exists, it will not be opened, fopen() will return false, and PHP will generate a warning.
fopen file mode: x+
Cautious write. Open the file for writing and reading, beginning from the start of the file. If the file already exists, it will not be opened, fopen() will return false, and PHP will generate a warning.
fopen file mode: a
Append. Open the file for appending (writing) only, starting from the end of the existing contents, if any. If it does not exist, try to create it.
fopen file mode: a+
Append. Open the file for appending (writing) and reading, starting from the end of the existing contents, if any. If it does not exist, try to create it.
fopen file mode: b
Binary. Used in conjunction with one of the other modes. You might want to use this mode if your file system differentiates between binary and text file. Windows systems differentiate; Unix systems do not. The PHP developers recommend you always us this option for maximum portability. It is the default mode.
fopen file mode: t
Text. Used in conjunction with one of the other modes. This mode is an option only in Windows systems. It is not recommended except before you have ported your code to work with the b option.
Define and Show: fwrite() or puts()
Write to a file.
fwrite($fp, $outputstring);
Define and Show: @
Error suppression operator is used to suppress Wordpresses default error message.
@ $fp = fopen(“$DOCUMENT_ROOT/bobs/orders.txt, ‘ab’);
Define and Show: file_put_contents()
This function writes the string to the file without any need for an fopen() function call.
Define and Show: fclose()
Close a file.
fclose($fp);
\n
New line.
\t
Tab.
Define and Show: feof()
File end of file. The feof() function takes a file handle as its single parameter. It returns true if the file pointer is at the end of the file.
while (!feof($fp)) {…
Define and Show: fgets()
This function reads one line at a time from a file until it has read 998 bytes from the file.
$order = fgets($fp, 999);
Define and Show: fgetss()
This function is similar to fgets() except that it strips out any PHP and HTML tags found in the string.
$order = fgets($fp, 999);
Define and Show: fgetscsv()
This function breaks up lines when you have used a delimiting character, such as the tab character or a comma.
$order = fgetcsv($fp, 100, “\t”);
Define and Show: readfile()
First option to read from a file. This function opens the file, echoes the content to the browser and then closes the file all in one go.
readfile(“$DOCUMENT_ROOT/bobs/orders.text”);
Define and Show: fpassthru()
Second option to read from a file. First open a file using fopen(). Then pass the file pointer as an argument to fpassthru(), which dumps the contents of the file from the pointer’s position onward to the browser. It closes the file when it is finished.
$fp = fopen(“$DOCUMENT_ROOT/bobs/orders.txt”, ‘rb’);
fpassthru($fp);
Define and Show: file()
Third option to read from a file. This function is identical to readfile() except that instead of echoing the file to the browser, it turns it into an array.
$filearray = file(“$DOCUMENT_ROOT/bobs/orders.txt”);
" : $char); } } ```
). echo nl2br(fread($fp, filesize($DOCUMENT_ROOT/bobs/orders.txt")));