chdir

  • The chdir function in PHP changes the current working directory.

    PHP

    chdir('/path/to/foo');

    Ruby’s Dir.chdir is equivalent.

    Ruby

    Dir.chdir('/path/to/foo')

    Temporarily Changing the Directory

    Often times, you’ll want to change to a directory to perform some operations and then return to the directory you started from. In PHP, you can do this by storing the current working directory in a variable before calling chdir.

    PHP

    $start = getcwd();
    chdir('/path/to/foo');
    // perform some operations
    chdir($start);

    Ruby’s blocks provide a convenient shortcut. If you pass a block to Dir.chdir, the directory will be changed before yielding to the block and then original working directory restored when the block exits.

    Ruby

    Dir.chdir('/path/to/foo') do
      # perform some operations
    end

    see also

Looking for Rails or PHP web application development, integration, and training?

Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.