View Full Version : php file over-ride help please
gwmbox
07-01-2010, 02:51 AM
I am trying to work out how I can get a file to check if another file exists and if so load that file instead of the one that the request is coming from.
So if I had a file
/includes/action.php
but I want it over-ridden if there is a file present at
/template/mytemplate/action.php
If there is then everything in /includes/action is ignored and everything in /template/mytemplate/action.php is used instead.
If there is nothing in the mytemplate folder then the original file is loaded.
I was paying around with file-sxists but could not get it work, I have this code;
$filename = SITE_ROOT.'/templates/'.TEMPLATE.'/action.php';
if (file_exists($filename)){
require_once($filename);
} else {
....
};
I also tried is_file and include but that did not work either.
Please help :)
bluewalrus
07-01-2010, 03:06 AM
TEMPLATE and SITE_ROOT are variables?
If so they be called with $TEMPLATE and $SITE_ROOT?
gwmbox
07-01-2010, 04:00 AM
No they are defines, e.g.
define('SITE_ROOT','/home/mydomain/public_html');
bluewalrus
07-01-2010, 04:05 AM
Okay, the code being executed is being called from the directory it is in.
So on my domain I have root/name1/file.php
If I'm looking for a file in name1 I can have
<?php
if(file_exists("filenameis.php")) {
echo "exists";
} else {
echo "Null";
}
?>
If I'm looking for it in the root though I need the ../ to go back one directory.
<?php
if(file_exists("../filenameis.php")) {
echo "exists";
} else {
echo "Null";
}
?>
So for your call function it is being read as
/home/mydomain/public_html/templates/'.TEMPLATE.'/action.php
but file exists is assuming you want to call this from the includes directory which you don't. You want to go back 1 directory then up 3 directories to check for that file, I think....
gwmbox
07-01-2010, 04:38 AM
Aha, that did it, I removed the DEFINES and entered the correct path and it works, but how do I get it to work with defines?
djr33
07-01-2010, 04:40 AM
TEMPLATE and SITE_ROOT are called constants (if that matters). They're like globals but can't ever be changed (just set once) and are always global: available inside functions and in the main scope.
One note: you can do what you are doing, but I like using the preset constant __FILE__ (two underscores before and after) to determine my paths: this uses the location of the current file (EVEN if it was included into another page) and automatically establishes the relative location if you use it as a base.
dirname(__FILE__) will give you the current directory (dirname() removes the filename).
dirname(__FILE__).'/subdirectory/' will give you a relative subdirectory.
dirname(__FILE__).'/../' will give you a the parent directory (it's a little odd to use ../ in the middle of a path, but I've used it many times and never had a problem*).
This isn't a correction to your code, just an idea that might help you for this or future projects.
(*If this bothers you, you can use this awkward alternative: dirname(dirname(__FILE__)).)
gwmbox
07-01-2010, 04:53 AM
Man this is bugging me, if I echo the defines I can see the results as correct, it for some reason is not working in the above code though....
@djr33 I played with your suggestions but that did not work for me (yet), still playing with it though.
gwmbox
07-01-2010, 05:22 AM
Thanks guys for all your help, I worked it out, I had another include to place before the code I was using, so I ended up with
require_once('../includes/myfile.php');
$siteroot = SITE_ROOT;
$mytemplate = TEMPLATE;
$filename = $siteroot.'/templates/'.$mytemplate.'/action.php';
if (file_exists($filename)){
require_once($filename);
} else {
Cheers
GW
djr33
07-01-2010, 05:41 AM
I don't think you need to use variables instead of constants.
SITE_ROOT.'/template.... should be fine.
It's not a problem but it is simpler to use only one thing (constants or variables).
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.