PHP Directory chdir() Function



The PHP Directory chdir() function is used to select the directory in which your script will execute. Or we can say that a PHP script runs at the installed place automatically.

But sometimes, we might need to work with files or folders that are present over other locations. So with the help of chdir(), we can switch to that location. This means that we can change the directory from where the script will look for files and execute the commands.

Syntax

Below is the syntax of the PHP Directory chdir() function −

bool chdir ( string $directory )

Parameters

Here are the required and optional parameters of the chdir() function −

Sr.No Parameter & Description
1

directory(Required)

The new current directory.

Return Value

The chdir() function returns TRUE on success or FALSE on failure.

PHP Version

PHP function chdir() was first introduced in core PHP 4 and work well with the PHP 5, PHP 7, PHP 8.

Example

In this PHP code we will simply change the present working directory using PHP Directory chdir() function. In PHP there is a function called getcwd() which is used to get the current working directory.

So first we will echo the present working directory then we will change the directory using chdir() function.

<?php
   // To know the current working directory
   echo getcwd() . "\n";

   // Using chdir() we can change current working directory
   chdir('html');

   // using the command we can get the updated working directory
   echo getcwd() . "\n";
?> 

Output

This will produce the following result −

/home/tutorialspoint
/home/tutorialspoint/html

Example

The chdir() function tries to change the current working directory to the specified path. If the directory change is successful chdir() function returns TRUE so the code inside the if block will get executed.

And suppose the if condition is failed so it will execute the else part in which the message will get printed "Failed to change the directory." See the code below −

<?php
   // Change the current directory to 'new dir'
   if (chdir('home/php/new dir')) {

       echo "Directory changed to 'new dir'.\n";
    
       // List all the files in the new directory 'new dir'
       $files = scandir(getcwd());
       foreach ($files as $file) {
           echo $file . "\n";
       }
   } else {
       echo "Failed to change the directory.";
   }
?> 

Output

This will generate the following result. In which the 'new dir' is the directory name we have changed using chdir() and index.txt and my.txt are the files present in the 'new dir' folder or directory −

Directory changed to 'new dir'.
.
..
index.txt
my.txt

Example

We will make use of the chdir() function to fix any problems and change the current directory. If the directory is not present, we will handle this situation using an if-else condition.

If the directory exists, the code in the if section will execute; if not, the code in the else section will run.

<?php
    $ourDir = 'unavailable_directory';

    // we will try to change the directory here in if section
    if (chdir($ourDir)) {
        echo "Directory changed to '$ourDir'. \n";
    } else {
        // if directory is not present in the system then print the message
        echo "Failed to change directory to '$ourDir'. Please check if the directory present.";
    }
?> 

Output

This will produce the below outcome −

Failed to change directory to 'unavailable_directory'. Please check if the directory present.

Example

In this example we will use chdir() function to navigate the directory to the upper and down location of the current directory.

After using chdir(".") to set the path to the current directory, it prints the current working directory again. Using chdir("../.."), it outputs the current working directory and changes it to the grandparent directory.

<?php
   echo "Initial directory: " . getcwd() . "\n";
   chdir(".");
   echo "After setting path to '.', the current directory is: " . getcwd() . "\n";
   chdir("../");
   echo "After setting path to '../', the current directory is: " . getcwd() . "\n";
   chdir("../../");
   echo "After setting path to '../../', the current directory is: " . getcwd() . "\n";
?> 

Output

This will produce the below result −

Initial directory: /Applications/XAMPP/xamppfiles/htdocs/mac
After setting path to '.', the current directory is: /Applications/XAMPP/xamppfiles/htdocs/mac
After setting path to '../', the current directory is: /Applications/XAMPP/xamppfiles/htdocs
After setting path to '../../', the current directory is: /Applications/XAMPP

Notes

  • The current directory is represented by a single dot (.) in the directory path, and the parent directory is represented by a double dot (..).
  • The chdir() function just changes directories-it doesn't actually create new directories.

Summary

The chdir() function, a built-in PHP directory function, is used to change the current directory. So you have an additional option for what you can do in the new directory.

php_function_reference.htm
Advertisements