Search code examples
phpmagentofopen

Magento failed require_once php file


I am trying to require_once a php file in Magento, so that i can use the functions inside the file. No matter where i place the php file, or where i try to require it, the same error occurs:

Fatal error: require_once(): Failed opening required 'http://127.0.0.1/ezzence/functions.php' (include_path='C:\wamp\www\ezzence\app\code\local;C:\wamp\www\ezzence\app\code\community;C:\wamp\www\ezzence\app\code\core;C:\wamp\www\ezzence\lib;.;C:\php\pear') in C:\wamp\www\ezzence\app\design\frontend\default\theme317\template\page\html\head.phtml on line 28

The PHP file only contains a single function, so the php file itself is not the problem. It's like PHP cant find my php file. I try to require it like this:

<?php require_once($this->getBaseUrl() . 'functions.php');?>

Originally i wanted the file to be in skin/theme/default/inc/, but for testing purpose i moved it to root.

I have checked that i have allow_url_fopen = On in my php.ini, but i dont know if that affects require_once method.

Any suggestions are welcome. Thanks in advance :)


Solution

  • you cannot include a php file over http. This is wrong:

    required once 'http://example.com/some_file.php'
    

    I think this is what you are looking for:

    require_once($this->getBaseDir() . 'functions.php'
    

    But this also is not the Magento way of doing it. You should create a module and place the function inside a helper. Then you can call the function without needing the require_once statement like this: Mage::helper('helper_alias_here')->doSomething()

    Edit and a bit off topic
    If you use Magento the proper way you don't need to use require statements unless you are rewriting a controller class.