dongmen1860
2014-06-19 15:40
浏览 38
已采纳

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');
?>

图片转代码服务由CSDN问答提供 功能建议

在我的一个magento模板phtml文件中,我试图包含一个单独的php文件。 当我包含它时我 得到任何输出,当我使用require而不是我得到以下错误

 致命错误:require():无法打开所需的'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')/ home / usr / public_html / app / design / frontend / theme / edits / template / 第30行上的review / product / view / list.phtml 
   
 
 

错误的第一行显示正确的url路径,当我直接转到它时,它可以工作 - 它只是不喜欢从phtml模板页面中包含/要求。

我在phtml文件中尝试了以下内容(使用magento的BaseURL,绝对路径和相对路径):

 &lt;?php 
 $ root = Mage :: getBaseUrl  (Mage_Core_Model_Store :: URL_TYPE_WEB); 
require / include($ root.'dir / test.php'); 
?&gt; 
 
&lt;?php 
require / include('http://www.site。  co.uk/dir/test.php');
?>

<?php 
require / include('../../../../../../ ..  /../../dir/test.php');
?>
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • double0201 2014-06-19 16:02
    已采纳

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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanfuxing2212 2014-06-19 15:45

    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?

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题