php遞歸遍歷刪除文件的方法

文章主要介紹了php遞歸遍歷刪除文件的方法,涉及php遍歷文件操作的相關技巧,非常具有實用價值,需要的朋友可以參考下.

php遞歸遍歷刪除文件的方法

本文實例講述了php遞歸遍歷刪除文件的'方法。分享給大家供大家參考。具體如下:

這個函數稍加修改就可以變成一個遞歸文件拷貝函數

?

1

2

3

4

5

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<?php

function mover($src,$dst) {

$handle=opendir($src);

// Opens source dir.

if (!is_dir($dst)) mkdir($dst,0755);

// Make dest dir.

while ($file = readdir($handle)) {

if (($file!=".") and ($file!="..")) {

// Skips . and .. dirs

$srcm=$src."/".$file;

$dstm=$dst."/".$file;

if (is_dir($srcm)) {

// If another dir is found

mover($srcm,$dstm);

// calls itself - recursive WTG

} else {

copy($srcm,$dstm);

unlink($srcm);

// Is just a copy procedure is needed

} // comment out this line

}

}

closedir($handle);

rmdir($src);

}

?>

希望本文所述對大家的php程序設計有所幫助。

  【拓展閱讀】

算法原理

如果用P表示n個元素的全排列,而Pi表示n個元素中不包含元素i的全排列,(i)Pi表示在排列Pi前面加上前綴i的排列,那麼n個元素的全排列可遞歸定義爲:

① 如果n=1,則排列P只有一個元素i;