dongmen1860
dongmen1860
2014-06-19 15:40

Magento - 在phtml模板文件中包含/要求php文件

已采纳

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');
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • double0201 double0201 7年前

    Instead of Mage::getBaseUrl use $root = Mage::getBaseDir();

    点赞 评论 复制链接分享
  • duanfuxing2212 duanfuxing2212 7年前

    require('http://....') is going to (if it's enabled) do a full-blown HTTP request to the specified URL. Since it's a URL, that webserver is going to EXECUTE the php script and send you its output.

    If you're trying to actually load the CODE of that test.php, e.g. the raw un-executed PHP, then you cannot use an http request. The remote server has NO idea that the http request is actually from PHP and is coming from in include(). It'll just blindly run that script and send over the output.

    You'd need to (say) rename the remote file to test.txt, so that the webserver sends it out as-is, and then that text will be executed as PHP code on YOUR server.

    And as far as the other paths go, if your $root is something like:

     /home/sites/example.com/html
    

    then the require is going to be looking for

     /home/sites/example.com/html/dir/test.php
    

    Is there a dir subdir inside whatever your site's $root is?

    点赞 评论 复制链接分享