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
deneweyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
0
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:
../yourfile.txt
c:\abc\www\yourfile.txt

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

hope this explains
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.

thanks
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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

Thanks
0
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.