{"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=\u201dphp\u201d manual=\u201dif%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\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<ul>\n<li>Both would return true on Unix systems \u2013 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 \u2018foo\u2019, which would prevent you from creating a directory name \u2018foo\u2019.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%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\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%0A%2F%2FThe%20name%20of%20the%20directory%20that%20we%20need%20to%20create.%0A%24directoryName%20%3D%20\u2019images\u2019%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\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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 \u201cfailed to open stream: No such file or directory\u201d will be occured.<\/li>\n<li>In Windows there is a difference between \u2018file\u2019 and \u2018folder\u2019 types, so need to use file_exists() and is_dir() at the same time, for ex.:<\/li>\n<\/ul>\n[pastacode lang=\u201djavascript\u201d manual=\u201dif%20(file_exists(\u2018file\u2019))%20%0A%7B%0A%20%20%20%20if%20(!is_dir(\u2018file\u2019))%20%0A%7B%20%0A%2F%2Fif%20file%20is%20already%20present%2C%20but%20it\u2019s%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(\u2018file\u2019)%3B%20%2F%2Ffor%20example%0A%20%20%20%20%20%20%20%20mkdir(\u2018file\u2019%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(\u2018file\u2019%2C%20NEEDED_ACCESS_LEVEL)%3B%0A%7D%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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=\u201djavascript\u201d manual=\u201d%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\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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(\u2026); (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=\u201djavascript\u201d manual=\u201dnamespace%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\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>\u00a0<\/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}]}}