Maison  >  Article  >  développement back-end  >  目录自定义函数,自定义函数_PHP教程

目录自定义函数,自定义函数_PHP教程

WBOY
WBOYoriginal
2016-07-13 10:00:55689parcourir

目录自定义函数,自定义函数

PHP中并不提供查询目录大小、复制目录、删除有文件的目录的函数,我们需要自己编写

这个函数只要传递一个url,就能递归查询目录中的文件大小,从而统计出目录大小

<span> 1</span> <?<span>php
</span><span> 2</span>     <span>function</span> dirSize(<span>$dir</span><span>){
</span><span> 3</span>         <span>$dirsize</span> = 0<span>;
</span><span> 4</span>         <span>$url</span> = <span>opendir</span>(<span>$dir</span><span>);
</span><span> 5</span>         <span>while</span>(<span>$filename</span> = <span>readdir</span>(<span>$url</span><span>)){
</span><span> 6</span>             <span>$file</span> = <span>$dir</span>.'/'.<span>$filename</span><span>;
</span><span> 7</span>             <span>if</span>(!(<span>$filename</span>=='.' || <span>$filename</span>=='..'<span>)){
</span><span> 8</span>                 <span>if</span>(<span>is_dir</span>(<span>$file</span><span>)){
</span><span> 9</span>                     <span>$dirsize</span> += dirSize(<span>$file</span><span>);
</span><span>10</span>                 }<span>else</span><span>{
</span><span>11</span>                     <span>$dirsize</span> += <span>filesize</span>(<span>$file</span><span>);
</span><span>12</span> <span>                }
</span><span>13</span> <span>            }
</span><span>14</span> <span>        }
</span><span>15</span>         <span>closedir</span>(<span>$url</span><span>);
</span><span>16</span>         <span>return</span> <span>$dirsize</span><span>;
</span><span>17</span>     }

 

这个函数需要传递两个url,分别是旧目录和新目录,就能复制该目录及文件

<span> 1</span> <?<span>php
</span><span> 2</span>     <span>function</span> copyDir(<span>$dirSrc</span>,<span>$dirTo</span><span>){
</span><span> 3</span>         <span>if</span>(<span>is_file</span>(<span>$dirTo</span><span>))
</span><span> 4</span> <span>        {
</span><span> 5</span>             <span>echo</span> '目标不是目录不能创建!'<span>;
</span><span> 6</span>             <span>return</span><span>;
</span><span> 7</span> <span>        }
</span><span> 8</span>         <span>if</span>(!<span>file_exists</span>(<span>$dirTo</span><span>))
</span><span> 9</span> <span>        {
</span><span>10</span>             <span>mkdir</span>(<span>$dirTo</span><span>);
</span><span>11</span> <span>        }
</span><span>12</span>         <span>$dir_handle</span> = @<span>opendir</span>(<span>$dirSrc</span><span>);
</span><span>13</span>         <span>if</span>(<span>$dir_handle</span><span>)
</span><span>14</span> <span>        {
</span><span>15</span>             <span>while</span>(<span>$filename</span> = <span>readdir</span>(<span>$dir_handle</span><span>))
</span><span>16</span> <span>            {
</span><span>17</span>                 <span>if</span>(<span>$filename</span>!="." && <span>$filename</span>!=".."<span>)
</span><span>18</span> <span>                {
</span><span>19</span>                     <span>$subSrcFile</span> = <span>$dirSrc</span> . "\\".<span>$filename</span><span>;
</span><span>20</span>                     <span>$subToFile</span> = <span>$dirTo</span> . "\\".<span>$filename</span><span>;
</span><span>21</span>                      
<span>22</span>                      <span>if</span>(<span>is_dir</span>(<span>$subSrcFile</span><span>))
</span><span>23</span> <span>                     {
</span><span>24</span>                          copyDir(<span>$subSrcFile</span>, <span>$subToFile</span><span>);
</span><span>25</span> <span>                     }
</span><span>26</span>                      <span>if</span>(<span>is_file</span>(<span>$subSrcFile</span><span>))
</span><span>27</span> <span>                     {
</span><span>28</span>                           <span>copy</span>(<span>$subSrcFile</span>, <span>$subToFile</span><span>);
</span><span>29</span> <span>                     }
</span><span>30</span> <span>                }
</span><span>31</span> <span>            }
</span><span>32</span>             <span>closedir</span>(<span>$dir_handle</span><span>);
</span><span>33</span> <span>        }
</span><span>34</span>     }

这个函数只要传递一个url,就能递归删除目录及子目录的文件,从而删除目录

<span> 1</span> <?<span>php
</span><span> 2</span> <span>function</span> rmDirAll(<span>$url</span><span>){
</span><span> 3</span>     <span>//</span><span>检测要打开目录是否存在</span>
<span> 4</span>     <span>if</span>(!<span>$dir</span>=@<span>opendir</span>(<span>$url</span><span>)){
</span><span> 5</span>         <span>return</span> <span>false</span><span>;
</span><span> 6</span> <span>    }
</span><span> 7</span>     <span>while</span>(<span>false</span> !==(<span>$file</span>=<span>readdir</span>(<span>$dir</span><span>))){
</span><span> 8</span>     <span>//</span><span>排除当前目录与父级目录</span>
<span> 9</span>         <span>if</span>(<span>$file</span>!=="."&&<span>$file</span>!==".."<span>){
</span><span>10</span>             <span>$file</span>=<span>$url</span> .'/'. <span>$file</span><span>;
</span><span>11</span>             <span>//</span><span>递归删除目录</span>
<span>12</span>             <span>if</span>(<span>is_dir</span>(<span>$file</span><span>)){
</span><span>13</span>                 rmDirAll(<span>$file</span><span>);
</span><span>14</span>             }<span>else</span><span>{
</span><span>15</span>                 <span>if</span>(@<span>unlink</span>(<span>$file</span><span>)){
</span><span>16</span>                     <span>continue</span><span>;
</span><span>17</span>                 }<span>else</span><span>{
</span><span>18</span>                     <span>return</span> <span>false</span><span>;
</span><span>19</span> <span>                }
</span><span>20</span> <span>            }
</span><span>21</span> <span>        }
</span><span>22</span>         <span>if</span>(@<span>rmdir</span>(<span>$url</span><span>)){
</span><span>23</span>            <span>return</span> <span>true</span><span>;
</span><span>24</span>         }<span>else</span><span>{
</span><span>25</span>            <span>return</span> <span>false</span><span>;
</span><span>26</span> <span>        }
</span><span>27</span> <span>    }
</span><span>28</span> }

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/973028.htmlTechArticle目录自定义函数,自定义函数 PHP中并不提供查询目录大小、复制目录、删除有文件的目录的函数,我们需要自己编写 这个函数只要传递一...
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn