Open In App

PHP | getcwd( ) Function

Improve
Improve
Like Article
Like
Save
Share
Report

The getcwd() function in PHP is an inbuilt function which is used to return the current working directory. This function does not accepts any parameter and returns the current working directory on successful function call or FALSE on failure.

Syntax:

getcwd()

Parameters: This function does not accept any parameter.

Return Value: It returns the current working directory on successful function call or FALSE on failure.

Errors And Exceptions:

  1. getcwd() returns FALSE on some Unix variants if any one of the parent directories does not have the readable or search mode set.
  2. If you try to use getcwd() in a directory that is a symbolic link, getcwd() gives you the target of that link.

Below programs illustrate the getcwd() function:

Program 1:




<?php
  
//current directory
$cur_dir = getcwd();
  
// displaying current directory
echo $cur_dir
?>


Output:

user/home/gfg

Program 2:




<?php
//current directory
 $cur_dir = getcwd();
  
echo("Current Directory is " . $cur_dir);
  
echo "<br>" ;
  
//changing current directory
$flag = chdir("user/home/articles");
  
if($flag == true) 
$cur_dir = getcwd();
echo("Directory Has Been Successfully Changed to " . $cur_dir);
}
else
{
echo("Failed to Change Directory.");
}
?>


Output:

Current Directory Location is user/home/gfg
Directory Has Been Successfully Changed to user/home/articles

Reference: http://php.net/manual/en/function.getcwd.php



Last Updated : 11 Jul, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads