{"id":11028,"date":"2021-12-06T15:35:18","date_gmt":"2021-12-06T15:35:18","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=11028"},"modified":"2021-12-06T15:35:30","modified_gmt":"2021-12-06T15:35:30","slug":"get-current-working-directory-php","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/get-current-working-directory-php\/","title":{"rendered":"How to get the Current Working Directory in PHP"},"content":{"rendered":"\n

There are several ways to get the current working directory in 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\n

The above example will output something similar to:<\/p>\n\n\n\n

\/path\/to\/folder<\/code><\/pre>\n\n\n\n

Working 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\n

Example:<\/p>\n\n\n\n

require_once dirname(__FILE__) . '\/folder\/script.php';\necho dirname($_SERVER[PHP_SELF]);<\/code><\/pre>\n\n\n\n

Either of the above examples will output something similar to:<\/p>\n\n\n\n

\/path\/to\/folder<\/code><\/pre>\n\n\n\n

Using 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\n

The above examples will output something similar to:<\/p>\n\n\n\n

1) folder\n2) to\n3) path<\/code><\/pre>\n\n\n\n

Conclusion<\/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":"\nHow to get the Current Working Directory in PHP<\/title>\n<meta name=\"description\" content=\"This guide shows you how to get the current working directory in PHP using one of getcwd(), dirname(), and basename() functions\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/local.brightwhiz\/get-current-working-directory-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to get the Current Working Directory in PHP\" \/>\n<meta property=\"og:description\" content=\"This guide shows you how to get the current working directory in PHP using one of getcwd(), dirname(), and basename() functions\" \/>\n<meta property=\"og:url\" content=\"http:\/\/local.brightwhiz\/get-current-working-directory-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Brightwhiz.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/brightwhiz\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-12-06T15:35:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-06T15:35:30+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/12\/current-working-directory-php.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Michael Bright\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:site\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michael Bright\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/local.brightwhiz\/get-current-working-directory-php\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/get-current-working-directory-php\/\"},\"author\":{\"name\":\"Michael Bright\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\"},\"headline\":\"How to get the Current Working Directory in PHP\",\"datePublished\":\"2021-12-06T15:35:18+00:00\",\"dateModified\":\"2021-12-06T15:35:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/get-current-working-directory-php\/\"},\"wordCount\":216,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/get-current-working-directory-php\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/12\/current-working-directory-php.jpg\",\"keywords\":[\"Open Source\",\"Optimization\",\"Performance\",\"PHP\",\"Programming Languages\",\"Software development\",\"Web Applications\",\"Web Development\"],\"articleSection\":[\"Articles\",\"Guides\",\"Technology\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/local.brightwhiz\/get-current-working-directory-php\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/local.brightwhiz\/get-current-working-directory-php\/\",\"url\":\"http:\/\/local.brightwhiz\/get-current-working-directory-php\/\",\"name\":\"How to get the Current Working Directory in PHP\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/get-current-working-directory-php\/#primaryimage\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/get-current-working-directory-php\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/12\/current-working-directory-php.jpg\",\"datePublished\":\"2021-12-06T15:35:18+00:00\",\"dateModified\":\"2021-12-06T15:35:30+00:00\",\"description\":\"This guide shows you how to get the current working directory in PHP using one of getcwd(), dirname(), and basename() functions\",\"breadcrumb\":{\"@id\":\"http:\/\/local.brightwhiz\/get-current-working-directory-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/local.brightwhiz\/get-current-working-directory-php\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/get-current-working-directory-php\/#primaryimage\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/12\/current-working-directory-php.jpg\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/12\/current-working-directory-php.jpg\",\"width\":1280,\"height\":680,\"caption\":\"Current Working Directory PHP\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/local.brightwhiz\/get-current-working-directory-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/local.brightwhiz\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to get the Current Working Directory in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/local.brightwhiz\/#website\",\"url\":\"http:\/\/local.brightwhiz\/\",\"name\":\"Brightwhiz.com\",\"description\":\"Best Tech guides, Tutorials, and News\",\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/local.brightwhiz\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/local.brightwhiz\/#organization\",\"name\":\"Brightwhiz\",\"url\":\"http:\/\/local.brightwhiz\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"width\":706,\"height\":135,\"caption\":\"Brightwhiz\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/brightwhiz\/\",\"https:\/\/x.com\/brightwhizmag\",\"https:\/\/instagram.com\/bright_whiz\/\",\"https:\/\/www.pinterest.com\/sobbayi\/\",\"https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\",\"name\":\"Michael Bright\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"caption\":\"Michael Bright\"},\"sameAs\":[\"https:\/\/sobbayi.com\"],\"url\":\"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to get the Current Working Directory in PHP","description":"This guide shows you how to get the current working directory in PHP using one of getcwd(), dirname(), and basename() functions","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/local.brightwhiz\/get-current-working-directory-php\/","og_locale":"en_US","og_type":"article","og_title":"How to get the Current Working Directory in PHP","og_description":"This guide shows you how to get the current working directory in PHP using one of getcwd(), dirname(), and basename() functions","og_url":"http:\/\/local.brightwhiz\/get-current-working-directory-php\/","og_site_name":"Brightwhiz.com","article_publisher":"https:\/\/www.facebook.com\/brightwhiz\/","article_published_time":"2021-12-06T15:35:18+00:00","article_modified_time":"2021-12-06T15:35:30+00:00","og_image":[{"width":1280,"height":680,"url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/12\/current-working-directory-php.jpg","type":"image\/jpeg"}],"author":"Michael Bright","twitter_card":"summary_large_image","twitter_creator":"@brightwhizmag","twitter_site":"@brightwhizmag","twitter_misc":{"Written by":"Michael Bright","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/local.brightwhiz\/get-current-working-directory-php\/#article","isPartOf":{"@id":"http:\/\/local.brightwhiz\/get-current-working-directory-php\/"},"author":{"name":"Michael Bright","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32"},"headline":"How to get the Current Working Directory in PHP","datePublished":"2021-12-06T15:35:18+00:00","dateModified":"2021-12-06T15:35:30+00:00","mainEntityOfPage":{"@id":"http:\/\/local.brightwhiz\/get-current-working-directory-php\/"},"wordCount":216,"commentCount":0,"publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"image":{"@id":"http:\/\/local.brightwhiz\/get-current-working-directory-php\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/12\/current-working-directory-php.jpg","keywords":["Open Source","Optimization","Performance","PHP","Programming Languages","Software development","Web Applications","Web Development"],"articleSection":["Articles","Guides","Technology"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/local.brightwhiz\/get-current-working-directory-php\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/local.brightwhiz\/get-current-working-directory-php\/","url":"http:\/\/local.brightwhiz\/get-current-working-directory-php\/","name":"How to get the Current Working Directory in PHP","isPartOf":{"@id":"http:\/\/local.brightwhiz\/#website"},"primaryImageOfPage":{"@id":"http:\/\/local.brightwhiz\/get-current-working-directory-php\/#primaryimage"},"image":{"@id":"http:\/\/local.brightwhiz\/get-current-working-directory-php\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/12\/current-working-directory-php.jpg","datePublished":"2021-12-06T15:35:18+00:00","dateModified":"2021-12-06T15:35:30+00:00","description":"This guide shows you how to get the current working directory in PHP using one of getcwd(), dirname(), and basename() functions","breadcrumb":{"@id":"http:\/\/local.brightwhiz\/get-current-working-directory-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/local.brightwhiz\/get-current-working-directory-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/get-current-working-directory-php\/#primaryimage","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/12\/current-working-directory-php.jpg","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/12\/current-working-directory-php.jpg","width":1280,"height":680,"caption":"Current Working Directory PHP"},{"@type":"BreadcrumbList","@id":"http:\/\/local.brightwhiz\/get-current-working-directory-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/local.brightwhiz\/"},{"@type":"ListItem","position":2,"name":"How to get the Current Working Directory in PHP"}]},{"@type":"WebSite","@id":"http:\/\/local.brightwhiz\/#website","url":"http:\/\/local.brightwhiz\/","name":"Brightwhiz.com","description":"Best Tech guides, Tutorials, and News","publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/local.brightwhiz\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/local.brightwhiz\/#organization","name":"Brightwhiz","url":"http:\/\/local.brightwhiz\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","width":706,"height":135,"caption":"Brightwhiz"},"image":{"@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/brightwhiz\/","https:\/\/x.com\/brightwhizmag","https:\/\/instagram.com\/bright_whiz\/","https:\/\/www.pinterest.com\/sobbayi\/","https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ"]},{"@type":"Person","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32","name":"Michael Bright","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","caption":"Michael Bright"},"sameAs":["https:\/\/sobbayi.com"],"url":"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/"}]}},"_links":{"self":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/11028"}],"collection":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/comments?post=11028"}],"version-history":[{"count":0,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/11028\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media\/11030"}],"wp:attachment":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media?parent=11028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/categories?post=11028"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/tags?post=11028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}