What does chdir() in PHP actually do?

This might seem like a stupid question but I've looked throught the PHP manual and whatever forum threads are available on the subject (not many) and I don't get what the chdir() function actually does. Does it change your include directory? I guess I don't get how you can change to a different directory than the one you are actually in. I have a fairly decent understanding of programming in PHP, but not so hot on dealing with manipulating directories, paths, etc. and am trying to learn, so please answer as if I don't know anything about it. Thanks
Who is Participating?
Guy Hengel [angelIII / a3]Billing EngineerCommented:
when you do some file operations from your php code, you can sepcify file names with or without a folder.
if you specify a file with relative folders OR without any folder specification, it will base on the current directory... which is by default the web server's folder or the web page's root, depending ...

so, if you don't manipulate any files in your code, you will never need this.
and honestly, I never needed chdir() in my applications at all, as I always refer to files with either absolute folder specs, or eventually relative folder spec.

say you are (currently) in folder c:\abc\www\site1
you can refer to the file c:\abc\www\yourfile.txt with either:

or do:
chdir("c:\abc\www\"); and then refer to yourfile.txt

hope this explains
Dave BaldwinFixer of ProblemsCommented:
"chdir()" changes your current working directory so that when you open a file like 'news.txt.' it will get it from the directory you changed to.  http://us2.php.net/manual/en/function.chdir.php
deneweyAuthor Commented:
Thank you both for your answers - both helped, but I'm left with the question:
Why use it? ... instead of a relative or absolute file reference.

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
as I wrote above: I never needed it.
deneweyAuthor Commented:
Thank you AngelIII, but do you know what the reason was for the creation of this function?

Guy Hengel [angelIII / a3]Billing EngineerCommented:
well, you can run php also for non-web applications, and eventually start other applications from your php code, which might rely on the fact on which is the current folder (as the called prog would inherit the "current folder" from the calling php app process) ... so I see where it could be useful. not sure if that was the creator's intention
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.