PHP working directory...

Discussion in 'Site Programming, Development and Design' started by jporkka, Feb 7, 2010.

  1. What cwd should a PHP script start with?

    I have a couple of scripts in forms\foo.php
    From the root directory page.php I include foo.php and call functions in it.

    I'm reading and writeing "report.txt".
    Sometimes I get the error "report.txt" is not found.

    So, from script I added:
    echo "Not Found " . $filename;
    echo getcwd();
    And I found that sometimes the cwd is C:\Windows\SysWOW64\inetsrv.

    For now I've hard coded my path to E:/web/MYSITE/forms/report.txt
    Surely, this isn't the right way to find the root of my site directory though.


    Why can a PHP script see anything in the filesystem outside of my own web site? Let alone have a default CWD in the system directory?
     
  2. Ray

    Ray

    This is a Microsoft Windows shared hosting platform. Because of security concerns we cannot give you access outside of your root espeically the servers operating files.
     
  3. I think you misunderstand.

    I do *NOT* want access to anything outside my little domain, yet it is being forced upon me.

    On my Winhost hosted site, sometimes, CWD==C:\Windows\Syswow64 -- not very helpful to me.


    From PHP, how can I reliably refer to the root of my directory tree?
     
    Last edited by a moderator: Oct 14, 2015
  4. Ray

    Ray

    Values for "getcwd" is not set or stored or cached on the server. Furthermore the php.ini file does not dictate this value.
     
  5. The results of getcwd() on a Windows/IIS server (and even some unix servers, depending on directory permissions) can be weird, and they are different when php is running as an Apache module, cgi wrapper, etc. There are just a lot of variables when it comes to php on Windows, and it makes troubleshooting things like this difficult if you haven't run into them before. I don't know why it's returning (what looks like) the process location, but I'm sure if you dig on the php site you'll find someone else seeing the same thing...
     
  6. I found what I needed.

    __FILE__ is the complete path+filename to the current PHP source file.
    dirname(__FILE__) returns just the path portion.


    Also, $_SERVER['DOCUMENT_ROOT'], is the root path to the site.
     
  7. Ray

    Ray

    Thanks this one is good to make a mental note on.
     
  8. Yeah, thanks for the follow-up.
     

Share This Page