Search code examples
phpcommand-line-interfacecd

Can I execute the "cd" command to change directories using PHP CLI?


I created a script using PHP CLI that I would like to have cd me into a directory based upon my command line input.

While I can get the PHP execution commands to echo output (e.g. echo ls -al), I cannot get them to run cd.

I have searched a lot online to find the solution, but have come up empty.


Solution

  • You can't use cd as it would run in a subshell, and the changed working directory would be lost when you returned to PHP before issuing the next command.

    Use chdir instead.