{"id":377,"date":"2014-07-04T07:26:32","date_gmt":"2014-07-04T07:26:32","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=377"},"modified":"2014-07-04T07:26:32","modified_gmt":"2014-07-04T07:26:32","slug":"php%e8%8e%b7%e5%8f%96%e7%9b%ae%e5%bd%95%e6%96%87%e4%bb%b6%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/377.html","title":{"rendered":"PHP\u83b7\u53d6\u76ee\u5f55\u6587\u4ef6\u5217\u8868"},"content":{"rendered":"<p>\u7528PHP\u83b7\u53d6\u6307\u5b9a\u76ee\u5f55\u7684\u6587\u4ef6\u5217\u8868\uff0c\u5e38\u89c1\u7684\u67092\u79cd\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\u4e3b\u8981\u7528\u5230PHP\u7684dir()\/is_dir()\u51fd\u6570\uff1b<\/li>\n<li>\u7528\u5230PHP\u7684opendir()\/readdir()\/closedir()\/realpath()\/is_dir()\u7b49\u51fd\u6570\u3002<\/li>\n<\/ol>\n<h6>\u65b9\u6cd5\u4e00\uff1a<\/h6>\n<pre class=\"lang:php decode:true\">&lt;?php\nprint_r(getDirFiles(\".\/\", true, true));\nprint_r(getDirFiles(\".\/\", true));\n\nfunction getDirFiles($path,$subDir=false\/*\u662f\u5426\u904d\u5386\u5b50\u76ee\u5f55*\/,$addDir=false\/*\u662f\u5426\u8f93\u51fa\u6587\u4ef6\u5939*\/) {\n\t$mydir=dir($path);\n\t$all=array();\n\twhile( ($file=$mydir-&gt;read())!==false){\n\t\tif($file==\".\" || $file==\"..\"){\n\t\t\tcontinue;\n\t\t}\n\t\tif ( is_dir( $path.$file .\"\/\") ) {\n\t\t\tif($addDir) {\n\t\t\t\t$all[]=$path.$file .\"\/\";\n\t\t\t}\n\t\t\tif($subDir) {\n\t\t\t\t$temp=getDirFiles( $path.$file .\"\/\" ,$sub );\n\t\t\t\t$all=array_merge($all,$temp);\n\t\t\t}\n\n\t\t} else {\n\t\t\t$all[]= $path.$file ;\n\t\t}\n\t}\n\treturn $all;\n}\n?&gt;<\/pre>\n<h6>\u65b9\u6cd5\u4e8c\uff1a<\/h6>\n<pre class=\"lang:php decode:true \">&lt;?php\n\/*\n*\u9012\u5f52\u83b7\u53d6\u6307\u5b9a\u8def\u5f84\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u6216\u5339\u914d\u6307\u5b9a\u6b63\u5219\u7684\u6587\u4ef6\uff08\u4e0d\u5305\u62ec\u201c.\u201d\u548c\u201c..\u201d\uff09\uff0c\u7ed3\u679c\u4ee5\u6570\u7ec4\u5f62\u5f0f\u8fd4\u56de\n*\t@param\tstring\t$dir\n*\t@param\tstring\t$pattern\n*\t@return\tarray\n*\/\nfunction file_list($dir,$pattern=\"\")\n{\n\t$arr=array();\n\t$dir_handle=opendir($dir);\n\tif($dir_handle)\n\t{\n\t\t\/\/ \u8fd9\u91cc\u5fc5\u987b\u4e25\u683c\u6bd4\u8f83\uff0c\u56e0\u4e3a\u8fd4\u56de\u7684\u6587\u4ef6\u540d\u53ef\u80fd\u662f\u201c0\u201d\n\t\twhile(($file=readdir($dir_handle))!==false)\n\t\t{\n\t\t\tif($file==='.' || $file==='..')\n\t\t\t{\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t$tmp=realpath($dir.'\/'.$file);\n\t\t\tif(is_dir($tmp))\n\t\t\t{\n\t\t\t\t$retArr=file_list($tmp,$pattern);\n\t\t\t\tif(!empty($retArr))\n\t\t\t\t{\n\t\t\t\t\t$arr[]=$retArr;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif($pattern===\"\" || preg_match($pattern,$tmp))\n\t\t\t\t{\n\t\t\t\t\t$arr[]=$tmp;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tclosedir($dir_handle);\n\t}\n\treturn $arr;\n}\n\n\/\/ \u5217\u51fa\u5f53\u524d\u76ee\u5f55\u4e0b\u6240\u6709\u4ee5\".php\"\u6269\u5c55\u540d\uff08\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff09\u7ed3\u5c3e\u7684\u6587\u4ef6\necho '&lt;pre&gt;';\nprint_r(file_list(dirname(__FILE__),'\/.php$\/i'));\necho '&lt;\/pre&gt;';\n?&gt;<\/pre>\n<h6>\u9644\u5f55\u4e00\uff1a\u7528PHP\u83b7\u53d6\u5f53\u524d\u76ee\u5f55\u7684\u65b9\u6cd5<\/h6>\n<pre class=\"lang:php decode:true \">&lt;?php\n\n\/\/ \u83b7\u53d6\u5f53\u524d\u76ee\u5f55\necho realpath(\".\").\"n\";\necho getcwd() . \"n\";\necho dirname(__file__) . \"n\";\t\/\/PHP \u5e38\u91cf\n\n\/\/ \u83b7\u53d6\u4e0a\u7ea7\u76ee\u5f55\necho realpath(\"..\"). \"n\";\necho dirname(dirname(__FILE__)) . \"n\";\t\/\/\u53d6\u5f97\u5f53\u524d\u6587\u4ef6\u7684\u4e0a\u4e00\u5c42\u76ee\u5f55\n\n\/\/ \u83b7\u53d6\u7f51\u7ad9\u6839\u76ee\u5f55\necho $_SERVER['DOCUMENT_ROOT'] . \"n\";\n\n\/\/ \u83b7\u53d6\u76ee\u5f55\u4fe1\u606f\n$path_parts = pathinfo(__FILE__);\necho $path_parts[\"dirname\"] . \"n\";\t\/\/\u76ee\u5f55\u540d\necho $path_parts[\"basename\"] . \"n\";\/\/\u6587\u4ef6\u540d\necho $path_parts[\"extension\"] . \"n\";\/\/\u6269\u5c55\u540d\necho dirname(__FILE__) . \"n\";\/\/\u76ee\u5f55\u540d--PHP\u5e38\u91cf\n\n?&gt;<\/pre>\n<h6>\u9644\u5f55\u4e8c\uff1a\u6279\u91cf\u91cd\u547d\u540d\u6587\u4ef6(\u53bb\u6389.bak\u7684\u540e\u7f00)<\/h6>\n<pre class=\"lang:php decode:true \">&lt;?php\n\/\/php\u6279\u91cf\u91cd\u547d\u540d\u6587\u4ef6\uff08\u53bb\u6389.bak\uff09\nfunction foreachDir($path){\n\tif(!is_dir($path)){\n\t\techo \"{$path}\u4e0d\u662f\u4e00\u4e2a\u6709\u6548\u7684\u76ee\u5f55\uff01\";\n\t\texit();\n\t}\n\t$handle=opendir($path);\n\tif($handle){\n\t\twhile (($file = readdir($handle))!==false) {\n\t\t\tif($file!='.' &amp;&amp; $file!='..'){\n\t\t\t\tif(is_dir($path.$file)){\n\t\t\t\t\techo $path.$file.\"&lt;br\/&gt;n\";\n\t\t\t\t\tforeachDir($path.$file);\n\t\t\t\t} else{\n\t\t\t\t\t$bak = pathinfo($file);\n\t\t\t\t\tif($bak[extension]==bak){\n\t\t\t\t\t\t$ext = strripos($file,'.');\n\t\t\t\t\t\t$newname = substr($file,0,$ext);\n\t\t\t\t\t\techo $path.'\/'.$file.'---'.$path.'\/'.$newname.\"&lt;br\/&gt;n\";\n\t\t\t\t\t\trename($path.'\/'.$file,$path.'\/'.$newname);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n}\n\nforeachDir('.\/');\n?&gt;<\/pre>\n<p>\u5dee\u4e0d\u591a\u5c31\u8fd9\u4e9b\u65b9\u6cd5\u4e86~<\/p>\n<h6>\u8865\u5145\uff1a<\/h6>\n<pre class=\"lang:php decode:true  \">function deleteDirAndFile( $dirName )\/\/\u9012\u5f52\u5220\u9664\u6307\u5b9a\u76ee\u5f55\n{\n\tif ( $handle = opendir( \"$dirName\" ) ) {\n\t\twhile (  ($item = readdir( $handle ))!==false  ) {\n\t\t\tif ( $item != \".\" &amp;&amp; $item != \"..\" ) {\n\t\t\t\tif ( is_dir( \"$dirName\/$item\" ) ) {\n\t\t\t\t\tdeleteDirAndFile( \"$dirName\/$item\" );\n\t\t\t\t} else {\n\t\t\t\t\t@unlink( \"$dirName\/$item\" );\/\/\u5220\u9664\u6587\u4ef6unlink()\n\t\t\t\t}\n\t\t\t}\n\t   }\n\t   closedir( $handle );\n\t   @rmdir( $dirName );\t\/\/rmdir()\u5220\u9664\u6307\u5b9a\u7684\u76ee\u5f55\uff0c\u8be5\u76ee\u5f55\u5fc5\u987b\u662f\u7a7a\u7684\uff0c\u800c\u4e14\u8981\u6709\u76f8\u5e94\u7684\u6743\u9650\n\t}\n}<\/pre>\n<h6>\u518d\u8865\u5145\u4e00\u4e2a\uff1a<\/h6>\n<pre class=\"lang:php decode:true \">&lt;?php\n\nfunction getDirFiles($path,$subDir=false\/*\u662f\u5426\u904d\u5386\u5b50\u76ee\u5f55*\/,$printDir=false\/*\u662f\u5426\u8f93\u51fa\u6587\u4ef6\u5939*\/) {\n\t$mydir=dir($path);\n\t$all=array();\n\twhile( ($file=$mydir-&gt;read())!==false){\n\t\tif($file==\".\" || $file==\"..\"){\n\t\t\tcontinue;\n\t\t}\n\t\tif ( is_dir( $path.DIRECTORY_SEPARATOR.$file) ) {\n\t\t\tif($printDir) {\n\t\t\t\t$all[]=$path.DIRECTORY_SEPARATOR.$file;\n\t\t\t}\n\t\t\tif($subDir) {\n\t\t\t\t$temp=getDirFiles( $path.DIRECTORY_SEPARATOR.$file, $subDir );\n\t\t\t\t$all=array_merge($all,$temp);\n\t\t\t}\n\n\t\t} else {\n\t\t\t$all[]= $path.DIRECTORY_SEPARATOR.$file ;\n\t\t}\n\t}\n\treturn $all;\n}\nprint_r(getDirFiles(getcwd()), true);\n\n\n\n$filelist = array();\nfunction get_file_list($dbsource) {\n\tglobal $filelist;\n\t$current_file_list = glob($dbsource);\n\tforeach ($current_file_list as $each) {\n\t\tif (strpos($each, 'search.php') === true)\n\t\t\tcontinue;\n\t\tif (is_file($each))\n\t\t\t$filelist[] = $each;\n\t\tif (is_dir($each))\n\t\t\tget_file_list($each . '\\*');\n\t}\n}\nget_file_list(getcwd() . '\\*');\nprint_r($filelist);\n\n\n\nfunction getDirFiles_r($path){\n\t$mydir = opendir($path);\n\t$all = array();\n\twhile ( false !== ($file=readdir($mydir)) ){\n\t\tif($file == \".\" || $file == \"..\"){\n\t\t\tcontinue;\n\t\t}\n\t\t$tmp = $path.DIRECTORY_SEPARATOR.$file;\n\t\tif( is_dir($tmp) ){\n\t\t\t$temp = getDirFiles_r($tmp);\n\t\t\t$all = array_merge($all, $temp);\n\t\t} else {\n\t\t\t$all[] = $tmp;\n\t\t}\n\t}\n\treturn $all;\n}\nprint_r(getDirFiles_r(getcwd()));\n\n?&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7528PHP\u83b7\u53d6\u6307\u5b9a\u76ee\u5f55\u7684\u6587\u4ef6\u5217\u8868\uff0c\u5e38\u89c1\u7684\u67092\u79cd\u65b9\u6cd5\uff1a \u4e3b\u8981\u7528\u5230PHP\u7684dir()\/is_dir()\u51fd\u6570\uff1b \u7528\u5230P [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,12],"tags":[183,184,185,186,48,187,188],"class_list":["post-377","post","type-post","status-publish","format-standard","hentry","category-programing","category-tools","tag-closedir","tag-dir","tag-is_dir","tag-opendir","tag-php","tag-readdir","tag-realpath"],"views":2244,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=377"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/377\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}