{"id":128,"date":"2021-03-08T00:25:30","date_gmt":"2021-03-08T00:25:30","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=128"},"modified":"2021-06-03T02:12:25","modified_gmt":"2021-06-03T02:12:25","slug":"php-directory","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-directory\/","title":{"rendered":"PHP Directory"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to work with directories using various built-in functions in PHP.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='basic-directory-operations'>Basic directory operations <a href=\"#basic-directory-operations\" class=\"anchor\" id=\"basic-directory-operations\" title=\"Anchor for Basic directory operations\">#<\/a><\/h2>\n\n\n\n<p>PHP provides a set of handy functions that allow you to work with directories effectively. To manage a directory, you need to get a directory handle.<\/p>\n\n\n\n<p>To get the directory handle of a directory, you pass the directory path to the <code>opendir()<\/code> function as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$dh = opendir(<span class=\"hljs-string\">'.\/public'<\/span>);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If an error occurs while opening the directory, the <code>opendir()<\/code> function returns <code>false<\/code>. <\/p>\n\n\n\n<p>When you&#8217;re done with the directory, you need to close the directory handle by using the <code>closedir()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\nclosedir($dh);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Each directory may contain a list of files or sub-directories. It may also contain the dot entry (<code>.)<\/code> that represents the current directory and the entry (<code>..<\/code>) that&nbsp;represents the parent directory.<\/p>\n\n\n\n<p>To get the next entry (a file or a directory) in a directory, you pass the directory handle to the <code>readdir()<\/code> function. <\/p>\n\n\n\n<p>Suppose the <code>public<\/code> directory is as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">.\n\u251c\u2500\u2500 <span class=\"hljs-selector-tag\">css<\/span>\n\u2502   \u2514\u2500\u2500 <span class=\"hljs-selector-tag\">style<\/span><span class=\"hljs-selector-class\">.css<\/span>\n\u2514\u2500\u2500 <span class=\"hljs-selector-tag\">js<\/span>\n    \u2514\u2500\u2500 <span class=\"hljs-selector-tag\">app<\/span><span class=\"hljs-selector-class\">.js<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example lists all subdirectories in the <code>public<\/code> directory:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$dh = opendir(<span class=\"hljs-string\">'.\/public'<\/span>);\n\n<span class=\"hljs-keyword\">if<\/span> ($dh) {\n\t<span class=\"hljs-keyword\">while<\/span> ($e = readdir($dh)) {\n\t\t<span class=\"hljs-keyword\">if<\/span> ($e !== <span class=\"hljs-string\">'.'<\/span> &amp;&amp; $e !== <span class=\"hljs-string\">'..'<\/span>) {\n\t\t\t<span class=\"hljs-keyword\">echo<\/span> $e , PHP_EOL;\n\t\t}\n\t}\n}\n\nclosedir($dh);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">css\njs<\/code><\/span><\/pre>\n\n\n<p>Note that the <code>readdir()<\/code> function returns the directories and files of the current directory specified by the directory handle. It doesn&#8217;t recursively returns the files and directories of the subdirectories.<\/p>\n\n\n\n<p>The following code is functionally equivalent to the above example except that it uses the <code><a href=\"https:\/\/phptutorial.net\/php-oop\/php-try-catch-finally\/\">try...catch...finally<\/a><\/code>  statement to handle the error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">try<\/span> {\r\n\t$dh = opendir(<span class=\"hljs-string\">'.\/public'<\/span>);\r\n\t<span class=\"hljs-keyword\">if<\/span> (!$dh) {\r\n\t\t<span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-keyword\">Exception<\/span>(<span class=\"hljs-string\">'Error openning the directory'<\/span>);\r\n\t}\r\n\t<span class=\"hljs-keyword\">while<\/span> ($e = readdir($dh)) {\r\n\t\t<span class=\"hljs-keyword\">if<\/span> ($e !== <span class=\"hljs-string\">'.'<\/span> &amp;&amp; $e !== <span class=\"hljs-string\">'..'<\/span>) {\r\n\t\t\t<span class=\"hljs-keyword\">echo<\/span> $e . <span class=\"hljs-string\">'&lt;br&gt;'<\/span>;\r\n\t\t}\r\n\t}\r\n} <span class=\"hljs-keyword\">catch<\/span> (\\Throwable $e) {\r\n\t<span class=\"hljs-keyword\">echo<\/span> $e-&gt;getMessage();\r\n} <span class=\"hljs-keyword\">finally<\/span> {\r\n\t<span class=\"hljs-keyword\">if<\/span> ($dh) {\r\n\t\tclosedir($dir);\r\n\t}\r\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='get-the-current-directory'>Get the current directory <a href=\"#get-the-current-directory\" class=\"anchor\" id=\"get-the-current-directory\" title=\"Anchor for Get the current directory\">#<\/a><\/h2>\n\n\n\n<p>By default, the current directory is the directory that contains the currently running script file. The current directory is important because it is used as the base directory for relative file paths.<\/p>\n\n\n\n<p>To get the current directory, you use the <code>getcwd()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">echo<\/span> getcwd();<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To change the current directory to a new one, you use the <code>chdir()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\nchdir(<span class=\"hljs-string\">'.\/dev'<\/span>);\n<span class=\"hljs-keyword\">echo<\/span> getcwd();<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>After calling the <code>chdir()<\/code> function, the current directory changes to &#8216;.\/dev&#8217;.  And you can verify it by calling the <code>getcwd()<\/code> function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='create-a-new-directory'>Create a new directory <a href=\"#create-a-new-directory\" class=\"anchor\" id=\"create-a-new-directory\" title=\"Anchor for Create a new directory\">#<\/a><\/h2>\n\n\n\n<p>To create a directory, you use the <code>mkdir()<\/code> function by passing the directory path. The <code>mkdir()<\/code> function returns <code>true<\/code> if it created the directory\u00a0successfully of <code>false<\/code> otherwise.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$dir = <span class=\"hljs-string\">'.\/public\/img'<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> (mkdir($dir)) {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">' The dir '<\/span>, $dir, <span class=\"hljs-string\">'created successfully!'<\/span>;\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Notice that the parent directory <code>public<\/code> must exist.<\/p>\n\n\n\n<p>By default the <code>mkdir()<\/code> function sets <code>0777<\/code> to the new directory. If you want to set different a permission, you can pass it to the <code>mkdir()<\/code> function or use the <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-file-permissions\/\">chmod()<\/a><\/code>\u00a0function. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$dir = <span class=\"hljs-string\">'.\/public\/assets'<\/span>;\nmkdir($dir, <span class=\"hljs-number\">0644<\/span>);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For more information on setting permissions on the file or directory, check it out the&nbsp;<a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-file-permissions\/\">file permissions<\/a>&nbsp;tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='delete-a-directory'>Delete a directory <a href=\"#delete-a-directory\" class=\"anchor\" id=\"delete-a-directory\" title=\"Anchor for Delete a directory\">#<\/a><\/h2>\n\n\n\n<p>To delete a directory, you use the <code>rmdir()<\/code> function. And you need to have sufficient permissions to remove the directory. <\/p>\n\n\n\n<p>Also, the directory needs to be empty. In other words, it doesn&#8217;t contain any files or sub-directories.<\/p>\n\n\n\n<p>The following example deletes the directory <code>assets<\/code> under the <code>public<\/code> directory:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\nrmdir(<span class=\"hljs-string\">'.\/public\/assets'<\/span>);<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='check-if-a-path-is-a-directory'>Check if a path is a directory <a href=\"#check-if-a-path-is-a-directory\" class=\"anchor\" id=\"check-if-a-path-is-a-directory\" title=\"Anchor for Check if a path is a directory\">#<\/a><\/h2>\n\n\n\n<p>To check if a path is a directory, you use the <code>is_dir()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">is_dir ( string $filename ) : bool<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>is_dir()<\/code> function returns <code>true<\/code> if the <code>$filename<\/code> exists and is a directory. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$path = <span class=\"hljs-string\">'.\/public'<\/span>;\n\n<span class=\"hljs-keyword\">if<\/span> (is_dir($path)) {\n\t<span class=\"hljs-keyword\">echo<\/span> $path, <span class=\"hljs-string\">' is a directory.'<\/span>;\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>opendir()<\/code> function to open a directory and get the directory handle and the <code>closedir()<\/code> function once you are done with the directory.<\/li><li>Use the <code>readdir()<\/code> function to read the entries in a directory specified by a directory handle.<\/li><li>Use the <code>mkdir()<\/code> function to create a new directory.<\/li><li>Use the <code>rmdir()<\/code> function to remove a directory.<\/li><li>Use the <code>is_dir()<\/code> function to check if a path is a directory and that directory exists in the file system.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"128\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-directory\/\"\n\t\t\t\tdata-post-title=\"PHP Directory\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"128\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-directory\/\"\n\t\t\t\tdata-post-title=\"PHP Directory\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to work with directories including open, close, create, remove, and check if a path is a directory.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":156,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-128","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=128"}],"version-history":[{"count":4,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/128\/revisions"}],"predecessor-version":[{"id":1910,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/128\/revisions\/1910"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}