Magic Constants Returning Null or Halfway

Asked

Viewed 122 times

0

Work on a Wordpress site using: PHP 7.0 and Microsoft-IIS/10.0.

The site is up and running, but occasionally errors appear in the log, usually errors related to Constants __DIR__ and __FILE__ coupled with require functions.

require (__DIR__.'/file.php');

require_once (__DIR__.'/file.php');

require ( dirname(__FILE__).'/file.php');

require_once ( dirname(__FILE__).'/file.php');

require (__DIR__ . DIRECTORY_SEPARATOR . 'arquivo.php');

The errors that are recorded are similar to these:

It shows only half the way that the __FILE__ should receive:

// Função: require_once dirname( __FILE__ ) . '/file.php';

Erro: PHP Fatal error:  require_once(): Failed opening required 'D:\home\site\wwwroot\wp-content\themes/file.php' (include_path='.;C:\php\pear\') in D:\home\site\wwwroot\wp-content\themes\l on line 22

__DIR__does not return any path:

//Função: require_once __DIR__ . '/file.php';
Erro: PHP Fatal error:  require_once(): Failed opening required '/file.php'
  • Are you willing to list the contents of the file ? Can you tell me better what you are trying to accomplish ?

  • I just do the file require. And the way it is now works perfectly, I did several tests debugging the whole process. However in production occasionally this error is recorded and when it occurs the site gets off the air.

  • Which version of PHP? This seems to me a configuration bug, using IIS?

  • I’m using IIS and it’s PHP 7.0

1 answer

1

Give a var_dump on __DIR__ . DIRECTORY_SEPARATOR . 'arquivo.php' and see what he returns.

If you copy the link that returned and that link, in the URL bar returns some file, the problem is another.


EDIT

Try instead of __DIR__ the dirname( __FILE__ )

  • It works, but for example 100 times, 1 fails and DIR returns nothing, so the path is incomplete. It is not a constant error.

  • How so 100 times 1 fails?

  • Exactly this my doubt. The command DIR returns the file path. But occasionally without apparent reasons (log records, or error msgs) it is not returning the path, causing require_once to search in the wrong directory.

  • Dude, really weird, sometimes it comes back sometimes doesn’t it? Try instead of DIR the dirname( FILE )

  • I edited my answer

  • I have tried tbm and the same error occurs. It occasionally does not return the path with dirname( FILE ). I am working on IIS, I do not know if it is a conflict. Everything you advised, works perfectly, the syntax of my code by everything I searched and studied is correct, but even so the error is repeated and the site carries only a part.

  • Dude, that part of the "site only loads once" I still don’t understand. Use require instead of require_once and test...

  • I’ve edited the question, but it’s usually as follows. The site is up and running, but at some point, which I can’t reproduce in my tests, __ DIR __ stops working and then at the time of require_once instead of receiving the (path + file.php) receives only (file.php). And it’s an extremely unusual mistake that makes no sense.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.