{"id":603,"date":"2022-09-08T11:02:46","date_gmt":"2022-09-08T03:02:46","guid":{"rendered":"https:\/\/www.djlog.cn\/?p=603"},"modified":"2022-09-08T11:02:46","modified_gmt":"2022-09-08T03:02:46","slug":"php-%e6%96%87%e4%bb%b6%e6%93%8d%e4%bd%9c%e5%ae%9e%e7%94%a8%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.djlog.cn\/?p=603","title":{"rendered":"PHP \u6587\u4ef6\u64cd\u4f5c\u5b9e\u7528\u4ee3\u7801"},"content":{"rendered":"<p><strong style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; color: #222222; font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-size: 16px; letter-spacing: 0.544px; text-align: justify; background-color: #ffffff; visibility: visible; box-sizing: border-box !important; overflow-wrap: break-word !important;\">1\u3001\u68c0\u6d4b\u76ee\u5f55\u662f\u5426\u5b58\u5728<\/strong><\/p>\n<pre class=\"language-php\"><code>function check_dir($path, $create = false) {\r\n      if (is_dir($path)) {\r\n          return true;\r\n      }\r\n      return false;\r\n}<\/code><\/pre>\n<p><strong style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; color: #222222; font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-size: 16px; letter-spacing: 0.544px; text-align: justify; background-color: #ffffff; box-sizing: border-box !important; overflow-wrap: break-word !important;\">2\u3001\u521b\u5efa\u76ee\u5f55<\/strong><\/p>\n<pre class=\"language-php\"><code>function create_dir($path) {\r\n      if (! file_exists($path)) {\r\n          if (mkdir($path, 0777, true)) {\r\n              return true;\r\n          }\r\n      }\r\n      return false;\r\n}<\/code><\/pre>\n<p><strong style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; color: #222222; font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-size: 16px; letter-spacing: 0.544px; text-align: justify; background-color: #ffffff; box-sizing: border-box !important; overflow-wrap: break-word !important;\">3\u3001\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/strong><\/p>\n<pre class=\"language-php\"><code>function check_file($path, $create = false, $content = null) {\r\n      if (file_exists($path)) {\r\n          return true;\r\n      } \r\n      return false;\r\n}<\/code><\/pre>\n<p><strong style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; color: #222222; font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-size: 16px; letter-spacing: 0.544px; text-align: justify; background-color: #ffffff; box-sizing: border-box !important; overflow-wrap: break-word !important;\">4\u3001\u521b\u5efa\u6587\u4ef6<\/strong><\/p>\n<pre class=\"language-php\"><code>function create_file($path, $content = null, $over = false) {\r\n      if (file_exists($path) &amp;&amp; ! $over) {\r\n          return false;\r\n      } elseif (file_exists($path)) {\r\n          @unlink($path);\r\n      }\r\n      check_dir(dirname($path), true);\r\n      $handle = fopen($path, 'w') or error('\u521b\u5efa\u6587\u4ef6\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5\u76ee\u5f55\u6743\u9650\uff01');\r\n      fwrite($handle, $content);\r\n      return fclose($handle);\r\n}<\/code><\/pre>\n<p><strong style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; color: #222222; font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-size: 16px; letter-spacing: 0.544px; text-align: justify; background-color: #ffffff; box-sizing: border-box !important; overflow-wrap: break-word !important;\">5\u3001\u76ee\u5f55\u6587\u4ef6\u5939\u5217\u8868<\/strong><\/p>\n<pre class=\"language-php\"><code>function dir_list($path) {\r\n      $list = array();\r\n      if (! is_dir($path) || ! $filename = scandir($path)) {\r\n          return $list;\r\n      }\r\n      $files = count($filename);\r\n      for ($i = 0; $i &lt; $files; $i ++) {\r\n          $dir = $path . '\/' . $filename[$i];\r\n          if (is_dir($dir) &amp;&amp; $filename[$i] != '.' &amp;&amp; $filename[$i] != '..') {\r\n              $list[] = $filename[$i];\r\n          }\r\n      }\r\n      return $list;\r\n}<\/code><\/pre>\n<p><strong style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; color: #222222; font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-size: 16px; letter-spacing: 0.544px; text-align: justify; background-color: #ffffff; box-sizing: border-box !important; overflow-wrap: break-word !important;\">6\u3001\u76ee\u5f55\u6587\u4ef6\u5217\u8868<\/strong><\/p>\n<pre class=\"language-php\"><code>function file_list($path) {\r\n      $list = array();\r\n      if (! is_dir($path) || ! $filename = scandir($path)) {\r\n          return $list;\r\n      }\r\n      $files = count($filename);\r\n      for ($i = 0; $i &lt; $files; $i ++) {\r\n          $dir = $path . '\/' . $filename[$i];\r\n          if (is_file($dir)) {\r\n              $list[] = $filename[$i];\r\n          }\r\n      }\r\n      return $list;\r\n}<\/code><\/pre>\n<p><strong style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; color: #222222; font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-size: 16px; letter-spacing: 0.544px; text-align: justify; background-color: #ffffff; box-sizing: border-box !important; overflow-wrap: break-word !important;\">7\u3001\u76ee\u5f55\u4e0b\u6587\u4ef6\u53ca\u6587\u4ef6\u5939\u5217\u8868<\/strong><\/p>\n<pre class=\"language-php\"><code>function path_list($path) {\r\n      $list = array();\r\n      if (! is_dir($path) || ! $filename = scandir($path)) {\r\n          return $list;\r\n      }\r\n      $files = count($filename);\r\n      for ($i = 0; $i &lt; $files; $i ++) {\r\n          $dir = $path . '\/' . $filename[$i];\r\n          if (is_file($dir) || (is_dir($dir) &amp;&amp; $filename[$i] != '.' &amp;&amp; $filename[$i] != '..')) {\r\n              $list[] = $filename[$i];\r\n          }\r\n      }\r\n      return $list;\r\n}<\/code><\/pre>\n<p><strong style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; color: #222222; font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-size: 16px; letter-spacing: 0.544px; text-align: justify; background-color: #ffffff; box-sizing: border-box !important; overflow-wrap: break-word !important;\"><strong style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 0.544px; box-sizing: border-box !important; overflow-wrap: break-word !important;\">8\u3001\u5220\u9664\u76ee\u5f55\u53ca\u76ee\u5f55\u4e0b\u6240\u6709\u6587\u4ef6\u6216\u5220\u9664\u6307\u5b9a\u6587\u4ef6<\/strong><\/strong><\/p>\n<pre class=\"language-php\"><code> \/**\r\n   * \u5220\u9664\u76ee\u5f55\u53ca\u76ee\u5f55\u4e0b\u6240\u6709\u6587\u4ef6\u6216\u5220\u9664\u6307\u5b9a\u6587\u4ef6\r\n   * @param str $path \u5f85\u5220\u9664\u76ee\u5f55\u8def\u5f84\r\n   * @param int $delDir \u662f\u5426\u5220\u9664\u76ee\u5f55\uff0ctrue\u5220\u9664\u76ee\u5f55\uff0cfalse\u5219\u53ea\u5220\u9664\u6587\u4ef6\u4fdd\u7559\u76ee\u5f55\r\n   * @return bool \u8fd4\u56de\u5220\u9664\u72b6\u6001\r\n   *\/\r\nfunction path_delete($path, $delDir = false, $exFile = array()) {\r\n      $result = true; \/\/ \u5bf9\u4e8e\u7a7a\u76ee\u5f55\u76f4\u63a5\u8fd4\u56detrue\u72b6\u6001\r\n      if (! file_exists($path)) {\r\n          return $result;\r\n      }\r\n      if (is_dir($path)) {\r\n          if (! ! $dirs = scandir($path)) {\r\n              foreach ($dirs as $value) {\r\n                  if ($value != \".\" &amp;&amp; $value != \"..\" &amp;&amp; ! in_array($value, $exFile)) {\r\n                      $dir = $path . '\/' . $value;\r\n                      $result = is_dir($dir) ? path_delete($dir, $delDir, $exFile) : unlink($dir);\r\n                  }\r\n              }\r\n              if ($result &amp;&amp; $delDir) {\r\n                  return rmdir($path);\r\n              } else {\r\n                  return $result;\r\n              }\r\n          } else {\r\n              return false;\r\n          }\r\n      } else {\r\n          return unlink($path);\r\n      }\r\n}<\/code><\/pre>\n<p><strong style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; color: #222222; font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; font-size: 16px; letter-spacing: 0.544px; text-align: justify; background-color: #ffffff; box-sizing: border-box !important; overflow-wrap: break-word !important;\"><strong style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; letter-spacing: 0.544px; box-sizing: border-box !important; overflow-wrap: break-word !important;\">9\u3001\u62f7\u8d1d\u6587\u4ef6\u5939<\/strong><\/strong><\/p>\n<pre class=\"language-php\"><code>function dir_copy($src, $des, $son = 1) {\r\n      if (! is_dir($src)) {\r\n          return false;\r\n      }      \r\n      if (! is_dir($des)) {\r\n          create_dir($des);\r\n      }      \r\n      $handle = dir($src);\r\n      while (! ! $path = $handle-&gt;read()) {\r\n          if (($path != \".\") &amp;&amp; ($path != \"..\")) {\r\n              if (is_dir($src . \"\/\" . $path)) {\r\n                  if ($son)\r\n                      dir_copy($src . \"\/\" . $path, $des . \"\/\" . $path, $son);\r\n              } else {\r\n                  copy($src . \"\/\" . $path, $des . \"\/\" . $path);\r\n              }\r\n          }\r\n      }\r\n      return true;\r\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u68c0\u6d4b\u76ee\u5f55\u662f\u5426\u5b58\u5728 function check_dir($path, $create = false) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[67],"class_list":["post-603","post","type-post","status-publish","format-standard","hentry","category-php","tag-67"],"_links":{"self":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=603"}],"version-history":[{"count":0,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/603\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}