PHP require_once fails but require works

64

In an application I am calling require_once $file where I have validated that the $file variable represents an absolute path, the file exists, and is readable. PHP 7.3 is giving me the dreaded: "Fatal error: require_once(): Failed opening required" message.

When I change the code to use "require" instead of "require_once" I get further, till the next "require_once" statement.

I am running PHP 7.2.19 on ubuntu 18.04.

This particular code is embedded deep within an application that has already included and required many different files at different times.

I have checked for obvious things like the particular file being included multiple times (it is not)... and including a different file for testing purposes, and that also consistently fails. I have double checked permissions, etc.

$file = "$destdir/lib/include.php";
$file = realpath($file);
if( is_file($file) && is_readable($file) ) {
    require_once $file;  // fails
    // require $file;  // works, but fails when require_once is called within this file.
    // include $file;   // works, but fails when require_once is called within this file
    // include_once $file; // fails
}


I expected of course, that the file is required.

People are also looking for solutions to the problem: php - An error When I want to validate null fileds and save them in laravel : "The GET method is not supported for this route. Supported methods: POST."

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.