SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,656
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    testing a directory?

    I'm trying to run a php script only if iim currently in 1 of 2 directories, so this is what I came up wit
    PHP Code:
    <?php 
    if (getcwd() == "recreation" || getcwd() == "service"  ) {
    $CSS=1; require("../calendar/calendar.php"); 
    } else {
    $CSS=1; require("calendar/calendar.php"); 
    }
    ?>
    What am I missing?

    Thx...
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,656
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thios works
    PHP Code:
    <?php 
     
    if ((getcwd() == "/home/fixmy1/public_html/coronadoshores/recreation") || (getcwd() == "/home/fixmy1/public_html/coronadoshores/service")) {
    require(
    "../calendar/calendar.php"); 
    } else {
    require(
    "calendar/calendar.php"); 
    }
    ?>
    "Oh, and Jenkins--apparently your mother died this morning."

  3. #3
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    This code is not very portable because you will have differnet absolute paths on different hosts. You can use basename() to overcome this problem:
    PHP Code:
    $dir basename(getcwd());
    if (
    $dir == "recreation" || $dir == "service"  ) {
    ... 
    Ideally, in this case you'd be better off defining the absolute base path for your application somewhere in a config file - either hard-coded or better yet, derived dynamically from __DIR__. Then you don't have such problems with includes and you don't need conditional statements, you just do this:
    PHP Code:
    require(BASE_ABS_DIR."calendar/calendar.php"); 

  4. #4
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,880
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    @lukeurtnowski ;

    Try this:

    PHP Code:

      $ff
    ='calendar/calendar.php'
      if(
    file_exists($ff)) {require($ff);}
      if(
    file_exists('../'.$ff)) {require('../'.$ff);}

    // or
      
    $ff='../calendar/calendar.php'
      if(
    file_exists($ff))
      {
        require(
    $ff);
      }
      else
      {
        require( 
    substr$ff2);
      } 
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014


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
  •