{"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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">if (!is_dir($dir)) <br\/>{<br\/>    mkdir($dir);         <br\/>}<br\/>Or should I also use file_exists? For example:<br\/><br\/>if (!file_exists($dir) &amp;&amp; !is_dir($dir)) <br\/>{<br\/>    mkdir($dir);         <br\/>} <\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">$dirname = $_POST[&quot;search&quot;];<br\/>$filename = &quot;\/folder\/&quot; . $dirname . &quot;\/&quot;;<br\/><br\/>if (!file_exists($filename)) <br\/>{<br\/>    mkdir(&quot;folder\/&quot; . $dirname, 0777);<br\/>    echo &quot;The directory $dirname was successfully created.&quot;;<br\/>    exit;<br\/>} <br\/>else <br\/>{<br\/>    echo &quot;The directory $dirname exists.&quot;;<br\/>}<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;?php<br\/><br\/>\/\/The name of the directory that we need to create.<br\/>$directoryName = &#039;images&#039;;<br\/><br\/>\/\/Check if the directory already exists.<br\/>if(!is_dir($directoryName)){<br\/>    \/\/Directory does not exist, so lets create it.<br\/>    mkdir($directoryName, 0755);<br\/>}<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">if (file_exists(&#039;file&#039;)) <br\/>{<br\/>    if (!is_dir(&#039;file&#039;)) <br\/>{ <br\/>\/\/if file is already present, but it&#039;s not a dir<br\/>        \/\/do something with file - delete, rename, etc.<br\/>        unlink(&#039;file&#039;); \/\/for example<br\/>        mkdir(&#039;file&#039;, NEEDED_ACCESS_LEVEL);<br\/>    }<br\/>}<br\/> else<br\/> { \/\/no file exists with this name<br\/>    mkdir(&#039;file&#039;, NEEDED_ACCESS_LEVEL);<br\/>}<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">$year = date(&quot;Y&quot;);   <br\/>$month = date(&quot;m&quot;);   <br\/>$filename = &quot;..\/&quot;.$year;   <br\/>$filename2 = &quot;..\/&quot;.$year.&quot;\/&quot;.$month;<br\/><br\/>if(file_exists($filename))<br\/>{<br\/>    if(file_exists($filename2)==false)<br\/>{<br\/>        mkdir($filename2,0777);<br\/>    }<br\/>}<br\/>else<br\/>{<br\/>    mkdir($filename,0777);<br\/>}<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">namespace com\\stackoverflow;<br\/><br\/>set_error_handler(function($errno, $errm) <br\/>{ <br\/>    if (strpos($errm,&quot;exists&quot;) === false) throw new \\Exception($errm); \/\/or better: create your own FolderCreationException class<br\/>}<br\/>);<br\/>mkdir($folder);<br\/>\/* possibly more mkdir instructions, which is when this becomes useful *\/<br\/>restore_error_handler();<\/code><\/pre> <\/div>\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}]}}