file() | Reads a file into an array |
fopen() | Opens a file or URL |
fread() | Reads from an open file(binary-safe) |
fwrite() | Writes to an open file(binary-safe) |
file
vsfile_get_contents
vs fopen
in PHPfile
- Reads entire file contents into an array of lines.file_get_contents
- Reads entire file content into a string.fopen
- Opens a file handler that can be manipulated with other library functions, but does not reading or writing itself.(...) The fopen
function does something entirely different - it opens a file descriptor, which functions as a stream to read or write the file. It is a much lower-level function, a simple wrapper around the C fopen
function, and simply calling fopen
won't do anything but open a stream.
🔗
A binary safe function is one that treats its input as a raw stream of bytes and ignores every textual aspect it may have. The term is mainly used in the PHP programming language to describe expected behaviour when passing birnary data into functions whose main responsibility is text and string manipulating, and is used widely in the official PHP documentation.(...)
Most programming languages let the programmer decide whether to parse the contents of a file as text, or read ist as binary data. To convey this intent, special flags or different functions exists when reading or writing files to disk.
For example, in the PHP programming language, developers have to use fopen($filename, "rb")
instead of fopen($filename, "r")
to read the files as a binary stream instead of interpreting the textual data as such. This may also be referred to as reading in 'binary safe' mode.
It means the function will work correctly when you pass it arbitrary binary data (i.e. strings containing non-ASCII bytes and/or null bytes).
For example, a non-binary-safe function might be based on a C function which expects null-terminated strings, so if the string contains a null character, the function would ignore anything after it.
This is relevant because PHP does not cleanly separate string and binary data.