{"id":111,"date":"2009-07-06T18:31:36","date_gmt":"2009-07-06T13:01:36","guid":{"rendered":"http:\/\/vikku.info\/codesnippets\/?p=111"},"modified":"2013-10-02T12:12:58","modified_gmt":"2013-10-02T06:42:58","slug":"dirname-and-__file__-to-find-parent-and-current-folder-of-a-path-or-php-script","status":"publish","type":"post","link":"http:\/\/vikku.info\/codesnippets\/php\/dirname-and-__file__-to-find-parent-and-current-folder-of-a-path-or-php-script\/","title":{"rendered":"dirname() and __FILE__ to find parent and current folder of a path or php script"},"content":{"rendered":"<p>__FILE__ is a magic constant along with __LINE__ and some more which i have not yet used.<\/p>\n<p>dirname() is a function which returns the directory name of a path.<\/p>\n<p>as we combine these two we get something interesting&#8230; at least for me.<\/p>\n<p>my problem was to find the current directory of the executing php script. i was not comfortable using dirname($_SERVER[&#8216;PHP_SELF&#8217;]) because i got only the folder name and not the complete path&#8230; <\/p>\n<p>and then i was gazing the pages online to find what could be the alternat&#8230; then it is all this __FILE__ and dirname functions which gives the complete parent folder path which i was in need of many times.<\/p>\n<p>to get current director&#8217;s full abs  path we can use the following<br \/>\ndirname(__FILE__)<br \/>\nand the parent directory full abs path we can use the following<br \/>\ndirname(dirname(__FILE__))<br \/>\nand it can go further&#8230;<\/p>\n<pre lang=\"php\">\r\n$dir = str_replace ( \u201c\\\u201d, \u201c\/\u201d, dirname ( _FILE_ ) )\r\n\/\/ for Windows based systems\r\n<\/pre>\n<p>&#8230;<\/p>\n<p>another use of __FILE__ is for debugging purpose while using queries.<\/p>\n<p>i have many queries executed for a page and if there was an error at any point then i have to waste time to find at which place it is unless you append some text like this to find the location (mysql_error().&#8221;while this query&#8221;)&#8230; <\/p>\n<p>so i tried using similar to this to get the line number of the error occured.<\/p>\n<pre lang=\"php\">\r\nmysql_query($query) or showError('status.php',mysql_error(),__LINE__,__FILE__);\r\n<\/pre>\n<p>if an error occured i will set the error in a session in the showError function and then i will redirect to a status page where it displays the error, line number and the filename where the error occured. and then clear the err string session to make it ready for the next error status&#8230;<\/p>\n<p>the following urls are good resources for dirname and __FILE__<br \/>\n<a href=\"http:\/\/php.net\/manual\/en\/function.dirname.php\"> php.net manual for function dirname<\/a><\/p>\n<p><a href='http:\/\/php.net\/manual\/en\/language.constants.predefined.php'>PHP Magic Constants<\/a><br \/>\n&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>__FILE__ is a magic constant along with __LINE__ and some more which i have not yet used. dirname() is a function which returns the directory name of a path. as we combine these two we get something interesting&#8230; at least for me. my problem was to find the current directory of the executing php script. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/111"}],"collection":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/comments?post=111"}],"version-history":[{"count":12,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/111\/revisions"}],"predecessor-version":[{"id":115,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/posts\/111\/revisions\/115"}],"wp:attachment":[{"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/media?parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/categories?post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/vikku.info\/codesnippets\/wp-json\/wp\/v2\/tags?post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}