0

how call .php outside the folder

my folder structurer is

image

I'm in cart.php

my code is

$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
 }

So my question is I want to get all data from the mail.php and assign it $tpl_file.

In above try I'm getting always NO with

A PHP Error was encountered

Severity: Warning

Message: file_get_contents(../mail.php): failed to open stream: No such file or directory

Filename: pages/cart.php

Line Number: 279

any Ideas??

2

9 Answers 9

4

Change

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

To

$tpl_file ='application/views/pages/mail.php';

NOTE: From your screenshot,mail.php is located in application/views/pages/mail.php

Sign up to request clarification or add additional context in comments.

Comments

1

Try to use full relative path to the file

$tpl_file = APPPATH.'views/pages/mail.php';

OR Try to Use

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

Comments

1

try changing

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

to

$tpl_file =__FILE__.'mail.php';

as both files are in the same directory then why relate the path to parent directory

6 Comments

file_get_contents(mail.php): failed to open stream: No such file or directory
you are in cart.php right and the cart.php and mail.php are in same folder ?
OS?? whats purpose of that??
Just tell me... there are issues with some OS and file permission and try adding application/views/pages/ in your path
this will not work because in view, codeigniter needs referance from index.php, instead __DIR__.'/mail.php' will work, -1 sorry
|
1

Codeigniter refers file and directories with referance to index.php located at root folder of your project.

So, You will get it by

$tpl_file = 'application/views/pages/mail.php';

or, if both are in same directory,

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

But that will be bad idea, instead assign it the Codeigniter way by,

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

So, your final code will be

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

if ($tpl_file)
{
    // do something
}

More info here.

Comments

0

Hope the following code will work for you.

$this->view('mail');

1 Comment

there is another way to load this from controller to. But this will do the job for you.
0

Try this

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

Comments

0

Since you are using CI, I would recommend using following code:

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

Comments

0

Use APPPATH for this

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

Comments

0

You can do it several way.

way 1#

$tpl_file =VIEWPATH.'pages/mail.php'
$msg_tmpl = file_get_contents($tpl_file);

mail.php is inside your view path so you can get value like this

way 2#

$msg_tmpl = $this->load->view('pages/mail',null,true);
//this will return the content instead of displaying it output.

Note

Your all php code runs through main index.php.So if you want to use relative path it should be relative with index.php not with cart.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.