如何调用另一个php文件到Codeigniter视图

如何调用另一个php文件到Codeigniter视图,php,codeigniter,Php,Codeigniter,如何在文件夹外调用.php 我的文件夹结构是 我在cart.php中 我的代码是 $tpl_file ='../mail.php';//problem is here LINE 279 $msg_tmpl = file_get_contents($tpl_file); if (!file_exists($tpl_file)) { ?> <script> alert('no'); </script> <?php } el

如何在文件夹外调用
.php

我的文件夹结构是

我在cart.php中

我的代码是

$tpl_file ='../mail.php';//problem is here LINE 279
$msg_tmpl = file_get_contents($tpl_file);
if (!file_exists($tpl_file))
   {
?>
   <script>
     alert('no');
   </script>
<?php
   }
   else
   {
   ?>
      <script>
         alert('yes');
      </script>
   <?php
 }
$tpl_file='../mail.php'//问题出在第279行
$msg\u tmpl=file\u get\u contents($tpl\u file);
如果(!file_存在($tpl_file))
{
?>
警告(“否”);
警惕(“是”);
试着改变

$tpl_file ='../mail.php';

既然两个文件都在同一个目录中,那么为什么要将路径与父目录关联起来呢

$tpl_file ='../mail.php';

注意:
从您的屏幕截图中,
mail.php
位于
application/views/pages/mail.php

尝试使用文件的完整相对路径

$tpl_file = APPPATH.'views/pages/mail.php';
或者尝试使用

 $tpl_file = $this->load->view('pages/mail',true);

Codeigniter引用位于项目根文件夹中的
index.php
文件和目录

所以,你会得到它的

$tpl_file = 'application/views/pages/mail.php';
或者,如果两者都在同一目录中

$tpl_file = __DIR__.'/mail.php';
但这不是个好主意,相反,将其指定为Codeigniter

$tpl_file = $this->load->view('pages/mail', '', true);
因此,您的最终代码将是

$tpl_file = $this->load->view('pages/mail', '', true);

if ($tpl_file)
{
    // do something
}

更多信息。

希望以下代码适用于您

$this->view('mail');
试试这个

$msg_tmpl = $this->load->view('mail', '', true);

由于您使用的是CI,我建议您使用以下代码:

<?php    
    $tmp_file = $this->load->view('pages/mail', array(), TRUE);
?>

为此使用
APPPATH

$msg_tmpl = file_get_contents(APPPATH.'views/pages/mail.php');

你可以用几种方法来做

方式1

php在您的视图路径中,因此您可以获得如下值

路2#

注意


您所有的php代码都通过main
index.php
运行。因此,如果您想使用相对路径,那么它应该与
index.php
相对,而不是与
cart.php

相对,其中包含mail.php的确切位置。请输入此
文件获取内容(mail.php):无法打开流:没有这样的文件或目录
您现在在cart.php中,cart.php和mail.php在同一个文件夹中?操作系统??这是为了什么?告诉我…某些操作系统和文件权限有问题,请尝试在您的路径中添加应用程序/视图/页面/这将不起作用,因为在视图中,codeigniter需要从
index.php
,而不是
\uuuuuu DIR\uuuuuuu.'/mail.php'
可以工作,-1抱歉,有另一种方法可以将此从控制器加载到。但这将为您完成此工作。
<?php    
    $tmp_file = $this->load->view('pages/mail', array(), TRUE);
?>
$msg_tmpl = file_get_contents(APPPATH.'views/pages/mail.php');
$tpl_file =VIEWPATH.'pages/mail.php'
$msg_tmpl = file_get_contents($tpl_file);
$msg_tmpl = $this->load->view('pages/mail',null,true);
//this will return the content instead of displaying it output.