Discussion:
what is the main difference between fread() and readfile()?
Negin Nickparsa
2014-08-10 22:31:13 UTC
Permalink
fread
(PHP 4, PHP 5)

fread — Binary-safe file read
Description ¶
<http://us1.php.net/manual/en/function.fread.php#refsect1-function.fread-description>
string fread ( resource $handle , int $length )

fread() reads up to length bytes from the file pointer referenced by handle.
Reading stops as soon as one of the following conditions is met:
readfile

(PHP 4, PHP 5)

readfile — Outputs a file
Description ¶
<http://us3.php.net/manual/en/function.readfile.php#refsect1-function.readfile-description>
int readfile ( string $filename [, bool $use_include_path = false [,
resource $context ]] )

Reads a file and writes it to the output buffer.

in which condition it's better to use them? I confronted a question that is
asking which of the following functions reads the entire contents of a file?

A.fgets

B.file_get_contents

C.fread()

D.readfile()

E.file()

in my opinion the answer is B,C,D,E

but the answer key is telling me the answer is B,D,E so fread for some
reason is not the answer and I cannot understand.

*john coggshall and Marco Tabini test book*
Aziz Saleh
2014-08-10 22:57:01 UTC
Permalink
Post by Negin Nickparsa
fread
(PHP 4, PHP 5)
fread — Binary-safe file read
Description ¶
<
http://us1.php.net/manual/en/function.fread.php#refsect1-function.fread-description
string fread ( resource $handle , int $length )
fread() reads up to length bytes from the file pointer referenced by handle.
readfile
(PHP 4, PHP 5)
readfile — Outputs a file
Description ¶
<
http://us3.php.net/manual/en/function.readfile.php#refsect1-function.readfile-description
int readfile ( string $filename [, bool $use_include_path = false [,
resource $context ]] )
Reads a file and writes it to the output buffer.
in which condition it's better to use them? I confronted a question that is
asking which of the following functions reads the entire contents of a file?
A.fgets
B.file_get_contents
C.fread()
D.readfile()
E.file()
in my opinion the answer is B,C,D,E
but the answer key is telling me the answer is B,D,E so fread for some
reason is not the answer and I cannot understand.
*john coggshall and Marco Tabini test book*
file_get_contents, file, and readfile all take the filename location as
oppose to fgets/fread which require file pointers - requiring you to open
the file first (via fopen for example).
Negin Nickparsa
2014-08-10 22:59:26 UTC
Permalink
Sincerely
Negin Nickparsa
Post by Aziz Saleh
Post by Negin Nickparsa
fread
(PHP 4, PHP 5)
fread — Binary-safe file read
Description ¶
<
http://us1.php.net/manual/en/function.fread.php#refsect1-function.fread-description
string fread ( resource $handle , int $length )
fread() reads up to length bytes from the file pointer referenced by handle.
readfile
(PHP 4, PHP 5)
readfile — Outputs a file
Description ¶
<
http://us3.php.net/manual/en/function.readfile.php#refsect1-function.readfile-description
int readfile ( string $filename [, bool $use_include_path = false [,
resource $context ]] )
Reads a file and writes it to the output buffer.
in which condition it's better to use them? I confronted a question that is
asking which of the following functions reads the entire contents of a file?
A.fgets
B.file_get_contents
C.fread()
D.readfile()
E.file()
in my opinion the answer is B,C,D,E
but the answer key is telling me the answer is B,D,E so fread for some
reason is not the answer and I cannot understand.
*john coggshall and Marco Tabini test book*
file_get_contents, file, and readfile all take the filename location as
oppose to fgets/fread which require file pointers - requiring you to open
the file first (via fopen for example).
​so fread is reading the entire file?​
Aziz Saleh
2014-08-11 00:21:22 UTC
Permalink
Post by Negin Nickparsa
Sincerely
Negin Nickparsa
Post by Aziz Saleh
Post by Negin Nickparsa
fread
(PHP 4, PHP 5)
fread — Binary-safe file read
Description ¶
<
http://us1.php.net/manual/en/function.fread.php#refsect1-function.fread-description
string fread ( resource $handle , int $length )
fread() reads up to length bytes from the file pointer referenced by handle.
readfile
(PHP 4, PHP 5)
readfile — Outputs a file
Description ¶
<
http://us3.php.net/manual/en/function.readfile.php#refsect1-function.readfile-description
int readfile ( string $filename [, bool $use_include_path = false [,
resource $context ]] )
Reads a file and writes it to the output buffer.
in which condition it's better to use them? I confronted a question that is
asking which of the following functions reads the entire contents of a file?
A.fgets
B.file_get_contents
C.fread()
D.readfile()
E.file()
in my opinion the answer is B,C,D,E
but the answer key is telling me the answer is B,D,E so fread for some
reason is not the answer and I cannot understand.
*john coggshall and Marco Tabini test book*
file_get_contents, file, and readfile all take the filename location as
oppose to fgets/fread which require file pointers - requiring you to open
the file first (via fopen for example).
​so fread is reading the entire file?​
No. fread reads a file pointer (not the file location). You can use
fread/fgets to read the entire file content, but you need to open a file
pointer first with fopen and know the size of the file with filesize.
Negin Nickparsa
2014-08-11 00:56:24 UTC
Permalink
Thank you Aziz


Sincerely
Negin Nickparsa
Post by Aziz Saleh
Post by Negin Nickparsa
Sincerely
Negin Nickparsa
Post by Aziz Saleh
Post by Negin Nickparsa
fread
(PHP 4, PHP 5)
fread — Binary-safe file read
Description ¶
<
http://us1.php.net/manual/en/function.fread.php#refsect1-function.fread-description
string fread ( resource $handle , int $length )
fread() reads up to length bytes from the file pointer referenced by handle.
readfile
(PHP 4, PHP 5)
readfile — Outputs a file
Description ¶
<
http://us3.php.net/manual/en/function.readfile.php#refsect1-function.readfile-description
int readfile ( string $filename [, bool $use_include_path = false [,
resource $context ]] )
Reads a file and writes it to the output buffer.
in which condition it's better to use them? I confronted a question that is
asking which of the following functions reads the entire contents of a file?
A.fgets
B.file_get_contents
C.fread()
D.readfile()
E.file()
in my opinion the answer is B,C,D,E
but the answer key is telling me the answer is B,D,E so fread for some
reason is not the answer and I cannot understand.
*john coggshall and Marco Tabini test book*
file_get_contents, file, and readfile all take the filename location as
oppose to fgets/fread which require file pointers - requiring you to open
the file first (via fopen for example).
​so fread is reading the entire file?​
No. fread reads a file pointer (not the file location). You can use
fread/fgets to read the entire file content, but you need to open a file
pointer first with fopen and know the size of the file with filesize.
Loading...