PHP<\/a> depending on what you are working on. Generally, you want to use these methods to programmatically find out which directory a running script is in.<\/p>\n\n\n\n Current Working Directory Using the getcwd() PHP Function<\/h2>\n\n\n\n This PHP function returns the current working folder on success and false on failure. The function is called with no parameters.<\/p>\n\n\n\n
On some Unix variants, getcwd() will return false if any one of the parent directories does not have the readable or search mode set, even if the current directory does.<\/p>\n\n\n\n
Example:<\/p>\n\n\n\n
echo getcwd();<\/code><\/pre>\n\n\n\nThe above example will output something similar to:<\/p>\n\n\n\n
\/path\/to\/folder<\/code><\/pre>\n\n\n\nWorking Directory Using dirname() PHP Function<\/h2>\n\n\n\n dirname function returns a parent directory’s path or depending on the parameters you can define how far up the parent path you want to return.<\/p>\n\n\n\n
To get the current directory using dirname() you need to add the PHP predefined FILE<\/strong> constant or PHP_SELF.<\/p>\n\n\n\nExample:<\/p>\n\n\n\n
require_once dirname(__FILE__) . '\/folder\/script.php';\necho dirname($_SERVER[PHP_SELF]);<\/code><\/pre>\n\n\n\nEither of the above examples will output something similar to:<\/p>\n\n\n\n
\/path\/to\/folder<\/code><\/pre>\n\n\n\nUsing basename() PHP function<\/h2>\n\n\n\n The basename function returns the trailing name component of path.<\/p>\n\n\n\n
Example:<\/p>\n\n\n\n
echo "1) ".basename("\/path\/to\/folder", ".d").PHP_EOL;\necho "2) ".basename("\/path\/to").PHP_EOL;\necho "3) ".basename("\/path\/").PHP_EOL;<\/code><\/pre>\n\n\n\nThe above examples will output something similar to:<\/p>\n\n\n\n
1) folder\n2) to\n3) path<\/code><\/pre>\n\n\n\nConclusion<\/h2>\n\n\n\n You have now learned three ways that you can get the current working directory in PHP.<\/p>\n","protected":false},"excerpt":{"rendered":"
There are several ways to get the current working directory in PHP depending on what you are working on. Generally, you want to use these methods to programmatically find out…<\/p>\n","protected":false},"author":1,"featured_media":11030,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,16],"tags":[424,433,449,452,472,544,636,638],"yoast_head":"\n
How to get the Current Working Directory in PHP<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n \n\t \n\t \n\t \n