{"id":1713,"date":"2016-11-05T12:26:46","date_gmt":"2016-11-05T12:26:46","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=1713"},"modified":"2025-09-21T10:34:25","modified_gmt":"2025-09-21T09:34:25","slug":"working-with-directories","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/files\/working-with-directories","title":{"rendered":"Working with directories"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Create directory<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\n  if (!is_dir('temp\/'))\n    mkdir('temp', 0755, true);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>define(\"MY_FILE_DOWNLOADS_DIRECTORY\",                      \"file-download\/\");\n\n  if (!is_dir($_SERVER&#91;'DOCUMENT_ROOT'] . \"\/\" . MY_FILE_DOWNLOADS_DIRECTORY))\n  {\n    mkdir($_SERVER&#91;'DOCUMENT_ROOT'] . \"\/\" . MY_FILE_DOWNLOADS_DIRECTORY, 0777, true);\n  }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Delete directory and its contents<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/*******************************************************\n\/\/*******************************************************\n\/\/********** DELETE DIRECTORY AND ITS CONTENTS **********\n\/\/*******************************************************\n\/\/*******************************************************\nfunction DeleteDirectoryAndContent ($DirectoryPath)\n{\n  if (is_dir($DirectoryPath))\n  {\n    $Files = scandir($DirectoryPath);\n    foreach ($Files as $File)\n    {\n      if ($File !== '.' &amp;&amp; $File !== '..')\n      {\n        $FilePath = $DirectoryPath . '\/' . $File;\n        if (is_dir($FilePath))\n          DeleteDirectoryAndContent($FilePath);\n        else\n          unlink($FilePath);\n      }\n    }\n    rmdir($DirectoryPath);\n  }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Delete directory contents only<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/****************************************************\n\/\/****************************************************\n\/\/********** DELETE DIRECTORY CONTENTS ONLY **********\n\/\/****************************************************\n\/\/****************************************************\nfunction DeleteDirectoryContentOnly ($DirectoryPath)\n{\n  if (is_dir($DirectoryPath))\n  {\n    $Files = scandir($DirectoryPath);\n    foreach ($Files as $File)\n    {\n      if ($File !== '.' &amp;&amp; $File !== '..')\n      {\n        $FilePath = $DirectoryPath . '\/' . $File;\n        if (is_dir($FilePath))\n          DeleteDirectoryAndContent($FilePath);\n        else\n          unlink($FilePath);\n      }\n    }\n  }\n}\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Delete old directory contents<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/*****************************************************************\n\/\/*****************************************************************\n\/\/********** DELETE DIRECTORY CONTENTS OLDER THAN # MINS **********\n\/\/*****************************************************************\n\/\/*****************************************************************\nfunction DeleteOldDirectoryContent($DirectoryPath, $DeleteOlderThanMins)\n{\n  if (!is_dir($DirectoryPath))\n    return;\n\n  $Files = scandir($DirectoryPath);\n  $TimeNow = time();\n  $MaxAgeSecs = $DeleteOlderThanMins * 60;      \/\/Seconds\n\n  foreach ($Files as $File)\n  {\n    if ($File===\".\" || $File===\"..\")\n      continue;\n\n    $FilePath=$DirectoryPath.DIRECTORY_SEPARATOR.$File;\n    if (is_file($FilePath))\n    {\n      $FileMTime = filemtime($FilePath);\n      if ($FileMTime !== False &amp;&amp; ($TimeNow - $FileMTime) > $MaxAgeSecs)\n      {\n        \/\/echo \"$FilePath&lt;br>\";\n        unlink($FilePath);\n      }\n    }\n  }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Create directory Delete directory and its contents Delete directory contents only Delete old directory contents<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-1713","post","type-post","status-publish","format-standard","hentry","category-files"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=1713"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1713\/revisions"}],"predecessor-version":[{"id":5111,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1713\/revisions\/5111"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=1713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=1713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=1713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}