Search code examples
phpwordpressreadfile

Include works, readfile doesn't


I'm experiencing odd behaviour loading a html file. require_once, include etc. works:

require_once 'suche.htm';

but readfile or file_get_contents won't

return file_get_contents('suche.htm');

Warning: readfile(suche.htm): failed to open stream: No such file or directory in /var/www/vhosts/h...


Solution

  • Time to read the fine manual

    Description
    int readfile ( string $filename [, bool $use_include_path = false [, resource $context ]] )

    Note the second argument

    use_include_path
    You can use the optional second parameter and set it to TRUE, if you want to search for the file in the include_path, too.

    file_get_contents has the same argument.

    You would need to set that to true if you want readfile / file_get_contents to use the same path resolution as require / include.


    Note, readfile does not return a string so you cannot use the result in htmlentities


    UPDATE

    In my opinion, it's much better to use an explicit file path, relative to the current PHP script file. For example

    file_get_contents(__DIR__ . '/../suche.htm');
    

    See http://php.net/manual/language.constants.predefined.php