PHP Tutorial - PHP chdir() function






Definition

You can change the working directory using chdir().

Syntax

PHP chdir() function syntax has the following syntax.

chdir(newDir)

Parameter

newDir is the new directory to change.

Return

chdir() returns true on success or false on failure.

Example

Change the working directory using chdir()

<?PHP
     $original_dir = getcwd();
     chdir("/etc");
     $passwd = fopen("passwd", "r");
     fclose($passwd);
     chdir($original_dir);
?>




Example 2

The following code shows how to change the current directory.


//ww w.ja v  a 2 s  . c o m
<?php
    // Get current directory
    echo getcwd() . "<br>";

    // Change directory
    chdir("images");

    // Get current directory
    echo getcwd();
?>