>  기사  >  백엔드 개발  >  目录自定义函数,自定义函数_PHP教程

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

WBOY
WBOY원래의
2016-07-13 10:00:55690검색

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

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中并不提供查询目录大小、复制目录、删除有文件的目录的函数,我们需要自己编写 这个函数只要传递一...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.