I'll quote some useful answers, because this question has been asked before:
First Kareem Zidade on a post on facebook:
You'll learn more about that in the following few weeks of the course,
but PHP is an "interpreted" language, meaning PHP code goes through a
program called an "interpreter", that interprets it, and produces
So what a web server, like ours, typically does upon receiving a
request for a PHP file is invoking a PHP interpreter, passing it the
file (or a path thereto) and possibly parameters, and maybe responding
with its output (if any).
If you look at the code for the
interpret function, you'll find it
popen to start a PHP interpreter process and create a "pipe"
(https://linux.die.net/man/7/pipe) to communicate with that process,
specifically expecting output from it, as denoted by the "r" passed as
a second arg to
For that reason, we cannot fseek through the data before reading it in
advance, and buffering it.
and then this stackoverflow answer:
The FILE returned by popen is not a regular file, but a thing called a
pipe. (That's what the p stands for.) Data flows through the pipe from
the stdout of the command you invoked to your program. Because it's a
communications channel and not a file on disk, a pipe does not have a
definite size, and you cannot seek to different locations in the data
stream. Therefore, fseek and ftell will both fail when applied to this
FILE, and that's what a -1 return value means. If you inspect errno
immediately after the call to ftell you will discover that it has the
value ESPIPE, which means "You can't do that to a pipe."
If you're trying to read all of the output from the command into a
single char* buffer, the only way to do it is to repeatedly call one
of the read functions until it indicates end-of-file, and enlarge the
buffer as necessary using realloc. If the output is potentially large,
it would be better to change your program to process the data in
chunks, if there's any way to do that.
If this is enough to clarify things, please click the check mark on the left to accept the answer.