Heim > php教程 > php手册 > 目录自定义函数,自定义函数

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

WBOY
Freigeben: 2016-06-13 09:10:02
Original
1062 Leute haben es durchsucht

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

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>     }
Nach dem Login kopieren

这个函数需要传递两个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>     }
Nach dem Login kopieren

这个函数只要传递一个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> }
Nach dem Login kopieren

 

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage