{"id":220,"date":"2020-12-12T22:00:00","date_gmt":"2020-12-12T22:00:00","guid":{"rendered":"https:\/\/rishikantsri.in\/blog\/?p=220"},"modified":"2023-09-22T08:32:39","modified_gmt":"2023-09-22T08:32:39","slug":"a-practical-guide-to-file-and-directory-manipulation-in-php-8","status":"publish","type":"post","link":"https:\/\/rishikantsri.in\/blog\/a-practical-guide-to-file-and-directory-manipulation-in-php-8\/","title":{"rendered":"A Practical Guide to File and Directory Manipulation in PHP 8"},"content":{"rendered":"\n<p>PHP is a versatile scripting language widely used in web development, and it provides a rich set of functions for working with files and directories. In this blog post, we&#8217;ll explore some of these functions in PHP 8, along with practical examples and expected outputs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. __FILE_<\/h2>\n\n\n\n<p>In PHP, you can use the <code>__FILE__<\/code> magic constant to obtain the current file&#8217;s absolute path. This constant provides the full path to the script that it&#8217;s used in. Here&#8217;s an example of how to use it:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.899999618530273px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"&lt;?php\n\n$file_path = __FILE__;\necho &quot;The path of current file is: &quot; .&quot;&lt;br&gt;&quot;. $file_path;\n\n?&gt;\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">&lt;?<\/span><span style=\"color: #B392F0\">php<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">$file_path <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">__FILE__<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">echo <\/span><span style=\"color: #FFAB70\">&quot;The path of current file is: &quot;<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">.<\/span><span style=\"color: #FFAB70\">&quot;&lt;br&gt;&quot;<\/span><span style=\"color: #F97583\">.<\/span><span style=\"color: #B392F0\"> $file_path;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">?&gt;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>When you run this code in a PHP script, it will output something like:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"947\" height=\"96\" src=\"https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-17.png\" alt=\"\" class=\"wp-image-222\" srcset=\"https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-17.png 947w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-17-300x30.png 300w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-17-768x78.png 768w\" sizes=\"(max-width: 947px) 100vw, 947px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. <code>pathinfo<\/code><\/h2>\n\n\n\n<p>The <code>pathinfo<\/code> function allows you to extract information about a file path. It returns an associative array containing details like the directory name, base name, extension, and filename.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.899999618530273px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"&lt;?php\n\n$path = __FILE__;\n$pathInfo = pathinfo($path);\necho &quot;&lt;pre&gt;&quot;;\nprint_r($pathInfo);\necho &quot;&lt;\/pre&gt;&quot;;\n\n?&gt;\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">&lt;?<\/span><span style=\"color: #B392F0\">php<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">$path <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">__FILE__<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">$pathInfo <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> pathinfo<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #B392F0\">$path<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">echo <\/span><span style=\"color: #FFAB70\">&quot;&lt;pre&gt;&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">print_r<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #B392F0\">$pathInfo<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">echo <\/span><span style=\"color: #FFAB70\">&quot;&lt;\/pre&gt;&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">?&gt;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"842\" height=\"260\" src=\"https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-18.png\" alt=\"\" class=\"wp-image-223\" srcset=\"https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-18.png 842w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-18-300x93.png 300w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-18-768x237.png 768w\" sizes=\"(max-width: 842px) 100vw, 842px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">3. <code>unlink<\/code><\/h2>\n\n\n\n<p><code>unlink<\/code> is used to delete a file.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:19.80000114440918px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"&lt;?php\n\n$fileToDelete = &quot;file_old.txt&quot;;\n\nif (unlink($fileToDelete)) {\n   echo &quot;File deleted successfully.&quot;;\n} else {\n   echo &quot;Unable to delete the file.&quot;;\n}\n\n?&gt;\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">&lt;?<\/span><span style=\"color: #B392F0\">php<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">$fileToDelete <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #FFAB70\">&quot;file_old.txt&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">if<\/span><span style=\"color: #B392F0\"> (unlink<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #B392F0\">$fileToDelete<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">   echo <\/span><span style=\"color: #FFAB70\">&quot;File deleted successfully.&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">} <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #B392F0\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">   echo <\/span><span style=\"color: #FFAB70\">&quot;Unable to delete the file.&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">?&gt;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Output (if the file exists and is deleted):<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"1012\" height=\"77\" src=\"https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-20.png\" alt=\"\" class=\"wp-image-225\" srcset=\"https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-20.png 1012w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-20-300x23.png 300w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-20-768x58.png 768w\" sizes=\"(max-width: 1012px) 100vw, 1012px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">4. <code>copy<\/code><\/h2>\n\n\n\n<p><code>copy<\/code>is used to copy a file from one location to another.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:19.80000114440918px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"&lt;?php\n\n$sourceFile = &quot;index.php&quot;;\n$destinationFile = &quot;about.php&quot;;\nif (copy($sourceFile, $destinationFile)) {\n   echo &quot;File copied successfully.&quot;;\n} else {\n   echo &quot;Unable to copy the file.&quot;;\n}\n\n?&gt;\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">&lt;?<\/span><span style=\"color: #B392F0\">php<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">$sourceFile <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #FFAB70\">&quot;index.php&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">$destinationFile <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #FFAB70\">&quot;about.php&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">if<\/span><span style=\"color: #B392F0\"> (copy<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #B392F0\">$sourceFile<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $destinationFile<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">   echo <\/span><span style=\"color: #FFAB70\">&quot;File copied successfully.&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">} <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #B392F0\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">   echo <\/span><span style=\"color: #FFAB70\">&quot;Unable to copy the file.&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">?&gt;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Output (if the copy is successful):<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"64\" src=\"https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-21-1024x64.png\" alt=\"\" class=\"wp-image-226\" srcset=\"https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-21-1024x64.png 1024w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-21-300x19.png 300w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-21-768x48.png 768w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-21.png 1032w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">5. <code>rename<\/code><\/h2>\n\n\n\n<p><code>rename<\/code> is used to rename a file or directory.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:19.80000114440918px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"\n&lt;?php\n\n$oldName = &quot;about.php&quot;;\n$newName = &quot;contact.php&quot;;\nif (rename($oldName, $newName)) {\n   echo &quot;File renamed successfully.&quot;;\n} else {\n   echo &quot;Unable to rename the file.&quot;;\n}\n\n?&gt;\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">&lt;?<\/span><span style=\"color: #B392F0\">php<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">$oldName <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #FFAB70\">&quot;about.php&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">$newName <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #FFAB70\">&quot;contact.php&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">if<\/span><span style=\"color: #B392F0\"> (rename<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #B392F0\">$oldName<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $newName<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">   echo <\/span><span style=\"color: #FFAB70\">&quot;File renamed successfully.&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">} <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #B392F0\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">   echo <\/span><span style=\"color: #FFAB70\">&quot;Unable to rename the file.&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">?&gt;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Output (if the rename is successful):<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"991\" height=\"66\" src=\"https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-22.png\" alt=\"\" class=\"wp-image-227\" srcset=\"https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-22.png 991w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-22-300x20.png 300w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-22-768x51.png 768w\" sizes=\"(max-width: 991px) 100vw, 991px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">6. <code>scandir<\/code><\/h2>\n\n\n\n<p><code>scandir<\/code> lists files and directories in a directory.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.899999618530273px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"&lt;?php\n\n$directory = __DIR__;\n$files = scandir($directory);\necho &quot;&lt;pre&gt;&quot;;\nprint_r($files);\necho &quot;&lt;\/pre&gt;&quot;;\n\n?&gt;\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">&lt;?<\/span><span style=\"color: #B392F0\">php<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">$directory <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">__DIR__<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">$files <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> scandir<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #B392F0\">$directory<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">echo <\/span><span style=\"color: #FFAB70\">&quot;&lt;pre&gt;&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">print_r<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #B392F0\">$files<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">echo <\/span><span style=\"color: #FFAB70\">&quot;&lt;\/pre&gt;&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">?&gt;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Output<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"275\" src=\"https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-23-1024x275.png\" alt=\"\" class=\"wp-image-228\" srcset=\"https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-23-1024x275.png 1024w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-23-300x81.png 300w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-23-768x206.png 768w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-23.png 1062w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">7. <code>is_dir \/ is_file<\/code><\/h2>\n\n\n\n<p>These functions check if a path is a directory or a file.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:19.80000114440918px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"&lt;?php\n\n$path = __DIR__;\n$path_file = __FILE__;\n\nif (is_dir($path)) {\n   echo &quot;It's a directory.&quot;;\n} elseif (is_file($path)) {\n   echo &quot;It's a file.&quot;;\n} else {\n   echo &quot;It's neither a directory nor a file.&quot;;\n}\n\n?&gt;\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">&lt;?<\/span><span style=\"color: #B392F0\">php<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">$path <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">__DIR__<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">$path_file <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">__FILE__<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">if<\/span><span style=\"color: #B392F0\"> (is_dir<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #B392F0\">$path<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">   echo <\/span><span style=\"color: #FFAB70\">&quot;It&#39;s a directory.&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">} <\/span><span style=\"color: #F97583\">elseif<\/span><span style=\"color: #B392F0\"> (is_file<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #B392F0\">$path<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">   echo <\/span><span style=\"color: #FFAB70\">&quot;It&#39;s a file.&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">} <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #B392F0\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">   echo <\/span><span style=\"color: #FFAB70\">&quot;It&#39;s neither a directory nor a file.&quot;<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">?&gt;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Output (based on the type of path):<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"903\" height=\"63\" src=\"https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-24.png\" alt=\"\" class=\"wp-image-229\" srcset=\"https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-24.png 903w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-24-300x21.png 300w, https:\/\/rishikantsri.in\/blog\/wp-content\/uploads\/2023\/09\/image-24-768x54.png 768w\" sizes=\"(max-width: 903px) 100vw, 903px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP is a versatile scripting language widely used in web development, and it provides a rich set of functions for working with files and directories. In this blog post, we&#8217;ll explore some of these functions in PHP 8, along with practical examples and expected outputs. 1. __FILE_ In PHP, you can use the __FILE__ magic [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,10],"tags":[23,4],"_links":{"self":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/220"}],"collection":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/comments?post=220"}],"version-history":[{"count":5,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/220\/revisions"}],"predecessor-version":[{"id":240,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/220\/revisions\/240"}],"wp:attachment":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/media?parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/categories?post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/tags?post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}