In one of my magento template phtml files I am trying to include a seperate php file. When I include it I get nothing outputted and when i use require instead I get the following error
Fatal error: require(): Failed opening required 'http://www.site.co.uk/dir/test.php'
(include_path='/home/usr/public_html/app/code/local:/home/usr/public_html/app/code/community:/home/usr/public_html/app/code/core:/home/usr/public_html/lib:.:/usr/lib/php:/usr/local/lib/php') in /home/usr/public_html/app/design/frontend/theme/edits/template/review/product/view/list.phtml on line 30
The first line of the error shows the correct url path and when i go to it directly it works - it just doesn't like being included/required from the phtml template page.
I've tried the following in the phtml file (using magento's BaseURL, absolute path and the relative path):
<?php
$root = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
require/include ($root.'dir/test.php');
?>
<?php
require/include ('http://www.site.co.uk/dir/test.php');
?>
<?php
require/include ('../../../../../../../../../dir/test.php');
?>
-
What is the reasoning behind including the file in this manor and not working it into a block? What does the file do?David Manners– David Manners2014年06月19日 18:19:05 +00:00Commented Jun 19, 2014 at 18:19
3 Answers 3
You can get installed directory by this code
Mage::getBaseDir()
So try like this
<?php require(Mage::getBaseDir().DS.'dir'.DS.'test.php');?>
You have to use the below code.
<?php
$root = getcwd();
require/include ($root.'dir/test.php');
?>
Now Test it will 100% work. When you call the file from url it will not included you have to include file use Base Directory Path so we have to use getcwd().
Should try Mage::getBaseDir('base') which will give magento dir
Also DS used Directory spectator
<?php require_once Mage::getBaseDir('base').DS.'dir'.DS.'test.php';?>
More details on http://alanstorm.com/magento_base_directories