Advanced Search

Results 1 to 9 of 9

Thread: php file over-ride help please

  1. #1
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    132
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default php file over-ride help please

    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;

    PHP 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
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    TEMPLATE and SITE_ROOT are variables?

    If so they be called with $TEMPLATE and $SITE_ROOT?
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    132
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    No they are defines, e.g.

    PHP Code:
    define('SITE_ROOT','/home/mydomain/public_html'); 
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

  4. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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 Code:
    <?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 Code:
    <?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....
    Last edited by bluewalrus; 07-01-2010 at 04:23 AM.
    Corrections to my coding/thoughts welcome.

  5. The Following User Says Thank You to bluewalrus For This Useful Post:

    gwmbox (07-01-2010)

  6. #5
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    132
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    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?
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

  7. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    261
    Thanked 690 Times in 678 Posts

    Default

    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__)).)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #7
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    132
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    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.
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

  9. #8
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    132
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    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

    PHP Code:
    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
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

  10. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    261
    Thanked 690 Times in 678 Posts

    Default

    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).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  11. The Following User Says Thank You to djr33 For This Useful Post:

    gwmbox (07-01-2010)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •