abspath()
function abspath()
{
echo $_SERVER['DOCUMENT_ROOT'];
}
directory()
function directory()
{
echo '/folder/';
}
Code Line:-
header('Location:'.abspath().directory());
Gives me the following as output:
C:/xampp/htdocs/folder/
When I use
header('Location:'.$_SERVER['DOCUMENT_ROOT'];.directory());
It sends me to my index.php in C:/xampp/htdocs/folder/index.php
Why its not working with functions?
I want to go to C:/xampp/htdocs/folder/index.php using this
header('Location:'.abspath().directory());
- Is there any problem?
The problem is that your functions are echoing your output and not returning it. You'll want to change your functions to:
function abspath()
{
return $_SERVER['DOCUMENT_ROOT'];
}
function directory()
{
return '/folder/';
}
So you can use the returned value (namely $_SERVER['DOCUMENT_ROOT'] or '/folder/', in this case) in your string concatentiation.
Your path should URI when using with header .
Soething like that
header('Location:http://yourpath.com/folder');
In the answer you are using a physical location where file residing
try $_SERVER["REQUEST_URI"]
Related
I got two files:
get_path.php:
function get_path() {
echo basename(__FILE__);
}
main.php
require("get_path.php");
get_path();
// This echos out "get_path.php", however i want it to echo out main.php
Does somebody know how to achieve this?
Thank you very much in advance!
Assuming that your function is as stated in the get_path.php file you can rewrite your function like this:
function get_path() {
$included_files = get_included_files();
foreach($included_files as $k=>$value){
if($value===__FILE__) return basename($included_files[$k-1]);
}
}
I use return but you can use echo and just after break; this return the file which directly include get_path.php but if you want the first level path.I mean the first file which include all the other files you can use :
function getFirstLevelPath(){
$included_files = get_included_files();
return basename($included_files[0]);
}
the output of your main will be now
:
main.php
I have very freak problem :-)
Please, see this simple website:
http://tests.vipserv.org/
There is... "1" number. But in code there is no any "1"...
I converted eol to Unix. No results. Also changed coding to UTF with/without boom.
"Website" is created from files:
index.php:
<?php
class View {
public static $TPL_VIEW = 'view.php';
public static function renderView($template, $data = array()) {
echo require $template;
}
public static function generateView($template, $data = array()) {
return require $template;
}
}
View::renderView(View::$TPL_VIEW, '');
?>
and view.php:
(blanc)
You can download files from:
http://ge.tt/4Td1TeI2/v/1
http://ge.tt/4Td1TeI2/v/0
Thanks,
A.
You are echoing it out yourself. According to the manual:
Handling Returns: include returns FALSE on failure and raises a
warning. Successful includes, unless overridden by the included file,
return 1.
So instead of:
echo require $template;
You probably want:
require $template;
Assuming that your template is not supposed to actually return anything of course.
echo require $template;
require returns if include succeeded or not i.e. true or false or 1 or 0.
so echo is displaying it.
have a file like so:
<?php
require_once "properties.php";
$GLOBALS["to_home"] = "../sfair";
echo tohome () . "index.php";
?>
And properties.php:
<?php
function tohome()
{
if(isset($GLOBALS['to_home']))
{
return $GLOBALS['to_home'] . "/";
}
else
{
return "';
}
}
Which should give me ../stair/index.php.
But instead gives me index.php.
How can I make the included file be able to access the variables of the including files?
note: I found another answer ("unable to access global variable from included file" but it did not work.
Can't you send the $GLOBALS["to_home"] with the Function call? That's what functions are for...
echo tohome($GLOBALS["to_home"]) . "index.php";
Of course you will have to change the function contents, but I guess you will get the idea.
I don't get why I get this error
Fatal error: Class 'ImageJpg' not found
Here is the code that I use
spl_autoload_register(function($class) {
if(file_exists($class))
{
include $class.'.php';
}
});
$n = new ImageJpg();
File ImageJpg.php is in the same dir with the code above.
Here is the content of ImageJpg.php
<?php
class ImageJpg
{
public function __construct()
{
echo 'Image from jpg called';
}
}
if(file_exists($class))
{
include $class.'.php';
}
Should be
if(file_exists($class.'.php'))
{
include $class.'.php';
}
Is there a class named ImageJpg in your ImageJpg.php file? And does file exist?
Try this:
spl_autoload_register(function($class) {
if(file_exists($class.'.php'))
{
include $class.'.php';
if (!class_exists($class))
{
die('required class not present');
}
}
else
{
die('file not found');
}
});
I had issues with file_exists also and I was debugging for like 2 hours and nothing happen even after I've searched all around and also on web. At the end it was a typo: instead userController.php it was userControlller.php the actual file.
Check again (in this order):
Make SURE you don't have typo in your file name (either PHP or file system)
Check your path - ROOT and/or use getcwd()
Temporary print / log your checks to make sure everything matches
Avoid human error :)
I understand that the question is rather hard to understand, I didn't know how to ask it better, so I'll use this code example to make things more clear:
If I have the following files:
test.php:
<?php
include('include.php');
echo myClass::myStaticFunction();
?>
include.php
<?php
__autoload($classname){
include_once("class/".$classname.".php"); //normally checking of included file would happen
}
?>
class/myClass.php
<?php
class myClass{
public static function myStaticFunction(){
//I want this to return test.php, or whatever the filename is of the file that is using this class
return SOMETHING;
}
?>
the magic FILE constant is not the correct one, it returns path/to/myClass.php
in case you need to get "test.php" see $_SERVER['SCRIPT_NAME']
I ended up using:
$file = basename(strtolower($_SERVER['SCRIPT_NAME']));
I am using
$arr = #debug_backtrace(false);
if (isset($arr))
foreach ($arr as $data)
{
if (isset($data['file']))
echo $data['file'];
// change it to needed depth
}
This way you don't need to modify the file from which your file is included. debug_backtrace might have some speed consenquencies.