{"id":3372,"date":"2017-04-01T18:06:51","date_gmt":"2017-04-01T12:36:51","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=3372"},"modified":"2017-04-01T18:06:51","modified_gmt":"2017-04-01T12:36:51","slug":"check-php-directory-exists","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/check-php-directory-exists\/","title":{"rendered":"[ Solved -6 Answers] PHP &#8211; How do we check with PHP if directory exists"},"content":{"rendered":"<p><label class=\"label label-warning\">PROBLEM:<\/label><\/p>\n<p>we want to create a directory if it does not exist. Is just using the is_dir function ok for that purpose? For example:<\/p>\n[pastacode lang=&#8221;php&#8221; manual=&#8221;if%20(!is_dir(%24dir))%20%0A%7B%0A%20%20%20%20mkdir(%24dir)%3B%20%20%20%20%20%20%20%20%20%0A%7D%0AOr%20should%20I%20also%20use%20file_exists%3F%20For%20example%3A%0A%0Aif%20(!file_exists(%24dir)%20%26%26%20!is_dir(%24dir))%20%0A%7B%0A%20%20%20%20mkdir(%24dir)%3B%20%20%20%20%20%20%20%20%20%0A%7D%20%0A&#8221; message=&#8221;php code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<ul>\n<li>Both would return true on Unix systems &#8211; in Unix everything is a file, including directories. But to test if that name is taken, you should check both.<\/li>\n<li>There might be a regular file named &#8216;foo&#8217;, which would prevent you from creating a directory name &#8216;foo&#8217;.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;%24dirname%20%3D%20%24_POST%5B%22search%22%5D%3B%0A%24filename%20%3D%20%22%2Ffolder%2F%22%20.%20%24dirname%20.%20%22%2F%22%3B%0A%0Aif%20(!file_exists(%24filename))%20%0A%7B%0A%20%20%20%20mkdir(%22folder%2F%22%20.%20%24dirname%2C%200777)%3B%0A%20%20%20%20echo%20%22The%20directory%20%24dirname%20was%20successfully%20created.%22%3B%0A%20%20%20%20exit%3B%0A%7D%20%0Aelse%20%0A%7B%0A%20%20%20%20echo%20%22The%20directory%20%24dirname%20exists.%22%3B%0A%7D%0A&#8221; message=&#8221;javascript code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[ad type=&#8221;banner&#8221;]\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<ul>\n<li>In this scenario, we want to create a folder called \u201cimages\u201d:<\/li>\n<\/ul>\n[pastacode lang=&#8221;php&#8221; manual=&#8221;%3C%3Fphp%0A%0A%2F%2FThe%20name%20of%20the%20directory%20that%20we%20need%20to%20create.%0A%24directoryName%20%3D%20&#8217;images&#8217;%3B%0A%0A%2F%2FCheck%20if%20the%20directory%20already%20exists.%0Aif(!is_dir(%24directoryName))%7B%0A%20%20%20%20%2F%2FDirectory%20does%20not%20exist%2C%20so%20lets%20create%20it.%0A%20%20%20%20mkdir(%24directoryName%2C%200755)%3B%0A%7D%0A&#8221; message=&#8221;php code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p><span style=\"color: #993300;\"><b>An explanation of the PHP code snippet above<\/b>:<\/span><\/p>\n<ul>\n<li>We set the name of the directory that we want to create. In this case, it is \u201cimages\u201d.<\/li>\n<li>We used PHP\u2019s is_dir function to check if the folder already exists. If the is_dir function returns a boolean FALSE value, then we know that the directory in question doesn\u2019t already exist.<\/li>\n<li>If the directory doesn\u2019t already exist, we can create it using the mkdir function.<\/li>\n<\/ul>\n<p><strong><span style=\"color: #000000;\">Note<\/span><\/strong> :that if you fail to check for the directory\u2019s existence, you run the risk of getting a nasty PHP warning:<\/p>\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n<ul>\n<li>If you already have file with the same name, but it is not a directory, !file_exists($dir) will return false, folder will not be created, so error &#8220;failed to open stream: No such file or directory&#8221; will be occured.<\/li>\n<li>In Windows there is a difference between &#8216;file&#8217; and &#8216;folder&#8217; types, so need to use file_exists() and is_dir() at the same time, for ex.:<\/li>\n<\/ul>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;if%20(file_exists(&#8216;file&#8217;))%20%0A%7B%0A%20%20%20%20if%20(!is_dir(&#8216;file&#8217;))%20%0A%7B%20%0A%2F%2Fif%20file%20is%20already%20present%2C%20but%20it&#8217;s%20not%20a%20dir%0A%20%20%20%20%20%20%20%20%2F%2Fdo%20something%20with%20file%20-%20delete%2C%20rename%2C%20etc.%0A%20%20%20%20%20%20%20%20unlink(&#8216;file&#8217;)%3B%20%2F%2Ffor%20example%0A%20%20%20%20%20%20%20%20mkdir(&#8216;file&#8217;%2C%20NEEDED_ACCESS_LEVEL)%3B%0A%20%20%20%20%7D%0A%7D%0A%20else%0A%20%7B%20%2F%2Fno%20file%20exists%20with%20this%20name%0A%20%20%20%20mkdir(&#8216;file&#8217;%2C%20NEEDED_ACCESS_LEVEL)%3B%0A%7D%0A&#8221; message=&#8221;javascript code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[ad type=&#8221;banner&#8221;]\n<p><label class=\"label label-info\">SOLUTION 5:<\/label><\/p>\n<h4 id=\"sample-code\"><span style=\"color: #ff6600;\"><b>Sample code:<\/b><\/span><\/h4>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;%24year%20%3D%20date(%22Y%22)%3B%20%20%20%0A%24month%20%3D%20date(%22m%22)%3B%20%20%20%0A%24filename%20%3D%20%22..%2F%22.%24year%3B%20%20%20%0A%24filename2%20%3D%20%22..%2F%22.%24year.%22%2F%22.%24month%3B%0A%0Aif(file_exists(%24filename))%0A%7B%0A%20%20%20%20if(file_exists(%24filename2)%3D%3Dfalse)%0A%7B%0A%20%20%20%20%20%20%20%20mkdir(%24filename2%2C0777)%3B%0A%20%20%20%20%7D%0A%7D%0Aelse%0A%7B%0A%20%20%20%20mkdir(%24filename%2C0777)%3B%0A%7D%0A&#8221; message=&#8221;javascript code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h4 id=\"solution-6\"><label class=\"label label-info\">SOLUTION 6:<\/label><\/h4>\n<ul>\n<li>Another option is to simply ignore the E_WARNING, not by using @mkdir(&#8230;); (because that would simply waive all possible warnings, not just the directory already exists one), but by registering a specific error handler before doing it:<\/li>\n<\/ul>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;namespace%20com%5Cstackoverflow%3B%0A%0Aset_error_handler(function(%24errno%2C%20%24errm)%20%0A%7B%20%0A%20%20%20%20if%20(strpos(%24errm%2C%22exists%22)%20%3D%3D%3D%20false)%20throw%20new%20%5CException(%24errm)%3B%20%2F%2For%20better%3A%20create%20your%20own%20FolderCreationException%20class%0A%7D%0A)%3B%0Amkdir(%24folder)%3B%0A%2F*%20possibly%20more%20mkdir%20instructions%2C%20which%20is%20when%20this%20becomes%20useful%20*%2F%0Arestore_error_handler()%3B%0A&#8221; message=&#8221;javascript code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How do we check with PHP if directory exists &#8211; Both would return true on Unix systems &#8211; in Unix is a file, including directories<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[275,25],"tags":[6257,3350,2134,6259,1204,1762,6258,6260,6261,404],"class_list":["post-3372","post","type-post","status-publish","format-standard","hentry","category-javascript","category-php","tag-check-whether-a-directory-exists-in-php","tag-how-can-i-prevent-sql-injection-in-php","tag-how-do-i-check-if-a-string-contains-a-specific-word-in-php","tag-how-do-you-use-bcrypt-for-hashing-passwords-in-php","tag-how-does-php-foreach-actually-work","tag-how-to-insert-if-not-exists-in-mysql","tag-is-there-any-function-to-check-if-a-directory-or-folder-exist-in-php","tag-php-checking-if-a-file-exists-in-a-directory","tag-php-directory-exist-check-problems","tag-reference-what-does-this-symbol-mean-in-php"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3372","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=3372"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3372\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=3372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=3372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=3372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}