Site Root directory?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rv!

    Site Root directory?

    I've posted this onto alt.php earlier and stumped all involved.
    I didn't cross post for the usual reasons Anyone here got any ideas?
    --------------------------------------


    I am simply attempting to find my sites root directory so I can then step up
    into a folder
    in which I will have my INCLUDE code. This will allow me to easily refer to
    my
    INCLUDE code without messing about.

    Now, This code...

    It simply attempts to put a file into the root of my server space, and
    recall it. It doesn't work.

    The file write doesn't error and the file read fails without error.
    Most importantly the file is nowhere to be seen!

    <Added when sent to the domain provider>
    --------------------------------------
    I am worried the file is dropping into a space not allocated to my account
    and that
    this could cause you/I some problems if it were to overwrite an important
    file.
    --------------------------------------
    </Added when sent to the domain provider>

    Robertv!






    <?

    $file="/tempfile.test";

    #write a file to the server.
    $thefile = fopen($file,"w" );
    fwrite ($thefile,"If you see this the file was successfully written to, and
    re-read from the server as filename: $file") ;
    fclose ($thefile);

    #read the file from the server.
    $thefile = fopen($file,"r" );
    $readdata=fread ($thefile,10000 0) ;
    fclose ($thefile);


    #display the results.
    echo "Data file when read was :<HR>";
    if($readdata==" ")
    {echo "Data came back empty. The file could not be read, and may not have
    been written."; }
    {echo $readdata."</HR>" ;}

    #
    #
    #User comments...
    # $file="tempfile .test"; will write to the current directory.
    # $file="../tempfile.test"; will write up one directory.
    # $file="/tempfile.test"; does some crazy disappearing act.
    # Server phpinfo() snip...Windows NT localhost 5.2 build 3790


  • Justin Koivisto

    #2
    Re: Site Root directory?

    Rv! wrote:[color=blue]
    >
    > I am simply attempting to find my sites root directory so I can then step up
    > into a folder
    > in which I will have my INCLUDE code. This will allow me to easily refer to
    > my
    > INCLUDE code without messing about.
    >
    > Now, This code...
    >
    > It simply attempts to put a file into the root of my server space, and
    > recall it. It doesn't work.
    >
    > The file write doesn't error and the file read fails without error.
    > Most importantly the file is nowhere to be seen!
    >
    > <Added when sent to the domain provider>
    > --------------------------------------
    > I am worried the file is dropping into a space not allocated to my account
    > and that
    > this could cause you/I some problems if it were to overwrite an important
    > file.
    > --------------------------------------
    > </Added when sent to the domain provider>
    >
    > Robertv!
    >
    >
    >
    >
    >
    >
    > <?
    >
    > $file="/tempfile.test";[/color]

    // Oops, first, this needs to be a system path, not a URI

    $file=$_SERVER['DOCUMENT_ROOT'].'/tempfile.test';
    [color=blue]
    > #write a file to the server.[/color]

    // should check if the file is writable
    // (this means that the file must exist and have proper rights on the
    // file system for the user that the webserver is running as)

    if(is_writable( $file)){
    [color=blue]
    > $thefile = fopen($file,"w" );
    > fwrite ($thefile,"If you see this the file was successfully written to, and
    > re-read from the server as filename: $file") ;
    > fclose ($thefile);[/color]

    }else{
    echo 'The file is now writable by the webserver user.';
    }


    // Should check if it is readable as well.
    if(is_readable( $file)){
    [color=blue]
    > #read the file from the server.
    > $thefile = fopen($file,"r" );
    > $readdata=fread ($thefile,10000 0) ;
    > fclose ($thefile);[/color]

    }else{
    echo 'Cannot read the file.';
    }
    [color=blue]
    > #display the results.
    > echo "Data file when read was :<HR>";
    > if($readdata==" ")[/color]

    // need to modify this line now
    if(!isset($read data))
    [color=blue]
    > {echo "Data came back empty. The file could not be read, and may not have
    > been written."; }
    > {echo $readdata."</HR>" ;}[/color]

    HTH

    --
    Justin Koivisto - spam@koivi.com
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.

    Comment

    • Rv!

      #3
      Re: Site Root directory?


      Hmm. This is bringing back the entire path to my PHP that called
      the lookup. ie.
      \\nas04ent\doma ins\m\<MYDOMAIN >\user\htdocs\< FIRST DIRECTORY>\soft \soft.php

      Interesting. I might just parse the file backwords until I get to the
      \htdocs\ parts and add
      a directory onthe end so it looks like
      \\nas04ent\doma ins\m\<MYDOMAIN >\user\htdocs\l ibrary
      where library is the directory I would like to call the include file from.
      Seems a lot of hassle as I believ the / should throw me to the root folder
      of my system,
      not the bleeding servers entire data set! :(

      I'm digging further now. Thanks for that input!

      Robert



      Comment

      • Rv!

        #4
        Re: Site Root directory?

        So.. I stripped back the servers response to the text...
        "\\nas04ent\dom ains\m\<MYDOMAI N>\user\htdocs "
        and I attacked the text "\library" and tried to write a file to
        it as...
        "\\nas04ent\dom ains\m\<MYDOMAI N>\user\htdocs\ library\test.fi le"
        and it failed with unwritable!

        It seems I can't find my ROOT by that method either.
        This is driving me nuts as it is virtually impossible for me to add my
        includes to point to a file in another folder. I have to sit there and
        count back down the folders and type in the right amount of
        "../../../../library/code.txt"
        until the bloody thing works!


        Sick sick sick sick sick. I'm off to get a beer.

        Robert



        Comment

        • Rv!

          #5
          Re: Site Root directory?

          I've been in discussion with the hosts who suggested I use their server
          path as the include.. \\NAS01\users\m \<DOMAIN>\htdoc s\...." etc
          which would force me to hardcode the PHP code and stop me testing it
          on anything other than their server. Instead if doing this I've written a
          shart
          directory crawler which will search out the include code.. It, and a message
          originally sent to alt.php are included below...




          Just thought about what they want me to do.
          I would need to hardcode my program to use their
          path command which would fail out on local tests or on moving to
          another server etc. I've decided not to use their system. Sat down
          for half an hours and wrote this simple function which will search
          back directories looking for the file or failing after a short search.

          It might not be great but it works on all tested systems (Their IIS Win2K3
          and my Apache Win2Kas). Please excuse the coding if it's bad, but I'm
          still learning this stuff! ;) Code is broken into little sections just to
          see it clearer
          when sending here.

          Hope this seems a reasonable solution to the more experienced folkseses?
          Any comments much appreciated.

          Robert



          <?
          #find the include file! Preset the $inc value to the part of the path
          we know.
          #strlen part sets a maximum path length to force exit if file search
          is unsuccessful.
          ?>
          <? $inc="library/_usefulcode.txt ";
          while(!file_exi sts($inc) && strlen($inc)<70 ) { $inc="../".$inc;}

          #check if the file actually exists or if we crashed out.
          if (!file_exists($ inc)) {die ("Include file could not be found in expected
          path.");}?>


          <?
          #Display the path we found our file in!
          echo "Include path is $inc so let's get it and use a function...<BR> ";
          require $inc;
          echo GETTIME();
          ?>


          Comment

          Working...