how call another php file to Codeigniter view - php

how call .php outside the folder
my folder structurer is
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??

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

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

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

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.

Hope the following code will work for you.
$this->view('mail');

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

Since you are using CI, I would recommend using following code:
<?php
$tmp_file = $this->load->view('pages/mail', array(), TRUE);
?>

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

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

Related

Error while including class from anthor file in php

I want to include a class from file own.php but I am not able to include it as it giving me error as require(class.own.php): failed to open stream: No such file or directory .
I tried all the options i.e include, require, require_once but then also it showing me a error.
include("class/own.php");
own.php
<?php
class own{
public function title(){
$title = $_POST['title'];
echo $title;
}
}
?>
display.php
include("class/own.php");
$obj = new own;
$obj->title();
Your directory structure should be like this
display.php
class/own.php
Then try to include
include("class/own.php");
The use
$obj = new own(); OR $obj = new \own();
$obj->title();
use this:
<?php
class own {
function title() {
$this->model = $_POST['title'];
}
}
// create an object
$test = new own();
// show object properties
echo $test->model;
?>
I think the two files are in the same path (directory), so you are including wrong path. It might be include("own.php");
Considering the following directory structure.
|directory
display.php
own.php
display.php
<?php
include("own.php");
$obj = new own;
$obj->title();
?>
I think you are confusing PHP. To include the file the following code may help
include (__DIR__)."/own.php";

require_once : failed to open stream

I read on SO and experiment with some answers but my code does not work:
I have two classes: C:\Apache24\htdocs\phpdb\classes\dbconnection\mysqlconnection\MySqlConnection.php
and C:\Apache24\htdocs\phpdb\classes\utilities\mysqlutilities\CreateTableDemo.php.
In CreateTableDemo I have the following code:
namespace utilities\mysqlutilities;
use dbconnection\mysqlconnection\MySqlConnection as MSC;
spl_autoload_register(function($class){
$class = 'classes\\'.$class.'.php';
require_once "$class";
});
I get the following warning:
`Warning: require_once(classes\dbconnection\mysqlconnection\MySqlConnection.php): failed to open stream: No such file or directory in C:\Apache24\htdocs\phpdb\classes\utilities\mysqlutilities\CreateTableDemo.php on line 10`.
I understand the warning, the script does not find the namespaced class in the same folder, so I changed the spl_autoload_register to look for a relative path: __DIR__."\\..\\..\\classes\\.$class.'.php'. I get the
warning: `Warning: require_once(C:\Apache24\htdocs\phpdb\classes\utilities\mysqlutilities\..\..\classes\dbconnection\mysqlconnection\MySqlConnection.php): failed to open stream: No such file or directory in C:\Apache24\htdocs\phpdb\classes\utilities\mysqlutilities\CreateTableDemo.php on line 10`.
I cannot find a way to direct the script to the namespaced class.
Thanks in advance for any help.
Create a autoloader-class in a separate file:
class Autoloader {
static public function loader($path) {
$filename = __DIR__.'/classes/'.str_replace("\\", '/', $path).".php";
if (file_exists($filename)) {
include($filename);
$aClass = explode('\\', $path);
$className = array_pop($aClass);
if (class_exists($className)) {
return TRUE;
}
}
return FALSE;
}
}
spl_autoload_register('Autoloader::loader');
And include it in you index file (or whatever).
It will load all your namespaced classes located in folder "classes".
require_once '/PATH_TO/autoload.php';
BTW: the trick is to replace the backslashes to regular slashes.
Works fine for me.
EDIT: Place the autoloader.php at same level like your "classes" folder is. :-)
It's failing because the path to the class is wrong relative to where you are requiring from. Try:
namespace utilities\mysqlutilities;
use dbconnection\mysqlconnection\MySqlConnection as MSC;
spl_autoload_register(function($class){
$exp = explode('classes', __DIR__);
$base = reset($exp);
$class = $base."classes".DIRECTORY_SEPARATOR.$class.".php";
require_once $class;
});

PHP Fatal error: Call to undefined function?

So i have a problem with my website when im hosting it on my webhost.
i get this error:
PHP Fatal error: Call to undefined function getSkillIcons()
The weird thing is that locally(Xampp) it works just fine.
This is how i included the function(in index.php):
<?php include 'http://sub.website.com/incl/Settings.php'; ?>
this is where i call it (index.php):
<div class="panel-body" style="text-align:center">
<?php getSkillIcons(explode(',', skills)); ?>
</div>
This how i defined skills (settings.php)
define("skills", "Test1,Test2,Test3,Test4");
this is the function itself: (settings.php)
function getSkillIcons($skills) {
echo '<img src="http://sub.website.com/incl/img/skill_icons/Overall-icon.png" class="skill-icon">';
for ($i = 0; $i < count($skills); $i++) {
$tooltip = 'title="'.$skills[$i].'" data-toggle="tooltip" data-placement="top"';
echo '';
}
Call to undefined function error clearly shows that its not getting your function where you have defined. Reason is you are attaching full path of settings.php file with http.
You need to include settings.php file without http path at the top of 'index.php' and make sure settings.php file is located in your project only. If it is located at the same folder with index.php, then simply include like below.
<?php include __DIR__.'/settings.php'; ?>
If your settings.php file is located in some other folder then you can use $_SERVER['DOCUMENT_ROOT'] to include that file like below:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/yourpath/settings.php";
include_once($path);
Also just an FYI for anyone trying to call a function within the same class in PHP, refer to the function like below:
return $this->doSomething($str);

get_included_files() for defined path

I am trying to get the names/path of the files that are included in a files with a given path . But the get_included_files() function works only for the files in which it is written.
<?
include_once('/folder1/folder2/xyz.php');
$path = '/abcd.php';
$includeFiles = get_included_files();
?>
I have tried the above code and i am getting only the included files in this file only.
I want to get the name of the included files in abcd.php i.e for the defined path.
Please HELP !!
How about something like this?
array_filter(get_included_files(), function($item) {
return strpos($item, '/path/to/files/') === 0;
});

Get calling file name from include()

I want to get the name of the file that includes another file from inside the included file.
I know there is the __FILE__ magic constant, but that doesn't help, since it returns the name of the included file, not the including one.
Is there any way to do this? Or is it impossible due to the way PHP is interpreted?
So this question is pretty old, but I was looking for the answer and after leaving here unsatisfied, I came across $_SERVER['SCRIPT_FILENAME']; Of course this works if the php file doing the including is a web page.
This gives you the full path of the "including file" on the server. eg /var/www/index.php. so if you want just the filename, eg index.php, you will need to use basename() eg
basename($_SERVER['SCRIPT_FILENAME']);
So, if in your index.php you have the following line:
<?php include("./somephp.php"); ?>
and in somephp.php you have
echo "this is the file that included me: " . basename($_SERVER['SCRIPT_FILENAME']);
you will get
this is the file that included me: index.php
output to the browser. This also works if the user is accessing your file without explicitly including the filename in the url, eg www.example.com instead of www.example.com/index.php.
Solution
Knowing that the functions used to include files are include, include_once, require and require_once, also knowing that they are reserved words in PHP, meaning that it will not be possible to declare user functions with the same name, and based on wedgwood's idea of using debug_backtrace you can actually work out from what file the include was called.
What we are going to do is iterate over the backtrace untill we find the most recent call to any of the four include functions, and the the file where it was called. The following code demostrate the technique:
function GetIncludingFile()
{
$file = false;
$backtrace = debug_backtrace();
$include_functions = array('include', 'include_once', 'require', 'require_once');
for ($index = 0; $index < count($backtrace); $index++)
{
$function = $backtrace[$index]['function'];
if (in_array($function, $include_functions))
{
$file = $backtrace[$index]['file'];
break;
}
}
return $file;
}
The above code will return the absolute path of the file where the last include happened, if there hasn't been any include it will return false. Note that the file may has been include from a file that was included from another file, the above function only works for the deepest include.
With a simple modification, you can also get the last included file:
function GetIncludedFile()
{
$file = false;
$backtrace = debug_backtrace();
$include_functions = array('include', 'include_once', 'require', 'require_once');
for ($index = 0; $index < count($backtrace); $index++)
{
$function = $backtrace[$index]['function'];
if (in_array($function, $include_functions))
{
$file = $backtrace[$index - 1]['file'];
break;
}
}
return $file;
}
Observations
Note that __FILE__ is not the included file, but the current file. For example:
file A.php:
<?php
function callme()
{
echo __FILE__;
}
?>
file B.php:
<?php
include('A.php');
callme();
?>
file index.php:
<?php
include('B.php');
?>
In the above example, in the context of the file B.php the included file is B.php (and the including file is index.php) but the output of the function callme is the path of A.php because __FILE__ is in A.php.
Also note that $_SERVER['SCRIPT_FILENAME'] will give the the absolute path to the script requested by the client. If $_SERVER['SCRIPT_FILENAME'] == __FILE__ it means that the current file is the requested one, and therefore there probably hasn't been any includes...
The above method checks if the current file is the requested one, but not if it hasn't been included (below is an example of how the requested file can be included). An actual solution to check if there has not been any inclusions could be to check count(get_included_files()) == 1.
The requested file can be an included file in the following way:
file x.php
<?php
$var = 'something';
include('index.php');
?>
file index.php
<?php
if (!isset($var))
{
include('x.php');
exit;
}
echo 'something';
?>
In the above example, the file index.php will include x.php, then x.php will include index.php back, afterwards index.php outputs "something" and returns control to x.php, x.php returns control to index.php and it reaches exit;
This shows that even if index.php was the requested script, it was also included from another script.
I can't find the easy way to cover this.
But If the including one is really important to you, you could hack it with some global variable and your own include function.
e.g.
<?php
$g_including_files = array();
function my_include($file) {
$bt = debug_backtrace();
global $g_including_files;
$g_including_files[basename($file)] = $bt[0]['file'];
return include($file);
}
May that be helpful for you :)
This is actually just a special case of what PHP templating engines do. Consider having this function:
function ScopedInclude($file, $params = array())
{
extract($params);
include $file;
}
Then A.php can include C.php like this:
<?php
// A.php
ScopedInclude('C.php', array('includerFile' => __FILE__));
Additionally, B.php can include C.php the same way without trouble.
<?php
// B.php
ScopedInclude('C.php', array('includerFile' => __FILE__));
C.php can know its includer by looking in the $params array.
<?php
// C.php
echo $includerFile;

Categories