Everything is working except for hello.php.

In the load function I try to obtain the length of the file passed to it as shown, but when hello.php is interpreted and the load function is called, it returns a length of zero for the generated file and thus a 500 Internal Server error when the content is read in the interpret function.

bool load(FILE* file, BYTE** content, size_t* length)
    // get file size
    int fileref = fileno(file);
    struct stat st;
    fstat(fileref, &st);
    *length = st.st_size;

    //make space for file content
    *content = malloc(sizeof(BYTE) * *length);

    //reading file into content
    fread(*content, sizeof(BYTE), *length, file);

    if (*content != NULL)
        return true;
        return false;


This seems particularly strange because when I check the file that gets passed into it via the interpret function by printing its characters, I see the following:

X-Powered-By: PHP/5.5.9-1ubuntu4.14
Content-type: text/html

<!DOCTYPE html>

                    hello, Bob            </body>

How could this return a length of zero?


1 Answer 1


That was my initial approach too. Sweet! Or so I thought.....See the section on "interpret" (around page 8) of the instructions.

Odds are you’re unfamiliar with popen. That function opens a "pipe" to a process (php-cgi in our case), which provides us with a FILE pointer via which we can read that process’s standard output (as though it were an actual file).

You cannot use fstat on a pipe (or ftell, fseek etc). It always returns 0. To be more specific, I spent a couple of hours with Mr. Google and couldn't find any way to do it. This post is an interesting discussion on the topic. (And this post too!)

Not the answer you're looking for? Browse other questions tagged .