The Wayback Machine - https://web.archive.org/web/20230422083127/https://www.geeksforgeeks.org/php-getcwd-function/
Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

PHP | getcwd( ) Function

Improve Article
Save Article
Like Article
author
Shubrodeep Banerjee
scholar
583 published articles
Improve Article
Save Article
Like Article

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


My Personal Notes arrow_drop_up
Last Updated : 11 Jul, 2018
Like Article
Save Article
Similar Reads
Related Tutorials