
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
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.