PHP常用技術文之檔案操作和目錄操作總結_PHP教程

來源:互聯網
上載者:User

PHP常用技術文之檔案操作和目錄操作總結


一、基本檔案的操作

檔案的基本操作有:檔案判斷、目錄判斷、檔案大小、讀寫性判斷、存在性判斷及檔案時間等

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

header("content-type","text/html;charset=utf-8");

/*

*聲明一個函數,傳入檔案名稱擷取檔案屬性

*@param string $fileName 檔案名稱

*/

function getFilePro($fileName)

{

if(!file_exists($fileName))

{

echo '檔案不存在
';

return;

}

/*是否是普通檔案*/

if(is_file($fileName))

{

echo $fileName.'是一個檔案
';

}

/*是否是目錄*/

if(is_dir($fileName))

{

echo $fileName.'是一個目錄';

}

/*輸出檔案的型態*/

echo '檔案型態是:'.getFileType($fileName).'
';

/*檔案大小,轉換單位*/

echo '檔案大小是:'.getFileSize(filesize($fileName)).'
';

/*檔案是否可讀*/

if(is_readable($fileName))

{

echo '檔案可讀
';

}

/*檔案是否可寫*/

if(is_writable($fileName))

{

echo '檔案可寫
';

}

/*檔案是否可執行*/

if(is_executable($fileName))

{

echo '檔案可執行
';

}

echo '檔案創立時間:'.date('Y年m月j日',filectime($fileName)).'
';

echo '檔案最後修改時間:'.date('Y年m月j日',filemtime($fileName)).'
';

echo '檔案最後開啟時間:'.date('Y年m月j日',fileatime($fileName)).'
';

}

/*

*聲明一個函數,返迴文件類型

*@param string $fileName 檔案名稱

*/

function getFileType($fileName)

{

$type = '';

switch(filetype($fileName))

{

case 'file':$type .= '普通檔案';break;

case 'dir':$type .= '目錄檔案';break;

case 'block':$type .= '塊裝置檔案';break;

case 'char':$type .= '字元裝置檔案';break;

case 'filo':$type .= '管道檔案';break;

case 'link':$type .= '符號連結';break;

case 'unknown':$type .= '未知檔案';break;

default:

}

return $type;

}

/*

*聲明一個函數,返迴文件大小

*@param int $bytes 檔案位元組數

*/

function getFileSize($bytes)

{

if($bytes >= pow(2,40))

{

$return = round($bytes / pow(1024,4),2);

$suffix = 'TB';

}

else if($bytes >= pow(2,30))

{

$return = round($bytes / pow(1024,3),2);

$suffix = 'GB';

}

else if($bytes >= pow(2,20))

{

$return = round($bytes / pow(1024,2),2);

$suffix = 'MB';

}

else if($bytes >= pow(2,10))

{

$return = round($bytes / pow(1024,1),2);

$suffix = 'KB';

}

else

{

$return = $bytes;

$suffix = 'B';

}

return $return." ".$suffix;

}

/*調用函數,傳入test目錄下的test.txt檔案*/

getFilePro('./test/div.html');

?>

結果:

二、目錄的操作

目錄的操作有:遍曆目錄、刪除、複製、大小統計等

1、遍曆目錄

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

/*

*遍曆目錄

*@param string $dirName 目錄名

*/

function findDir($dirName)

{

$num = 0; /*統計子檔案個數*/

$dir_handle = opendir($dirName); /*開啟目錄*/

/*輸出目錄檔案*/

echo '

echo '

echo '

echo '

while($file = readdir($dir_handle))

{

$dirFile = $dirName.'/'.$file;

$bgcolor = $num++%2==0?'#ffffff':'#cccccc';

echo '

echo '

echo '

echo '

echo '

echo '

}

echo "

'; '; '; '; '; '; '; '; ';

目錄'.$dirName.'下的檔案

檔案名稱 檔案大小 檔案類型 修改時間
'.$file.''.filesize($dirFile).''.filetype($dirFile).''.date('Y/n/t',filemtime($dirFile)).'
";

closedir($dir_handle); /*關閉目錄*/

echo "在".$dirName."目錄下共有".$num.'個子檔案';

}

/*傳遞目前的目錄下的test目錄*/

findDir('./test');

結果

2、統計目錄大小

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

/*

*統計目錄大小

*@param string $dirName 目錄名

*@return double

*/

function dirSize($dirName)

{

$dir_size = 0;

if($dir_handle = @opendir($dirName))

{

while ($fileName = readdir($dir_handle))

{

/*排除兩個特殊目錄*/

if($fileName != '.' && $fileName != '..')

{

$subFile = $dirName.'/'.$fileName;

if(is_file($subFile))

{

$dir_size += filesize($subFile);

}

if(is_dir($subFile))

{

$dir_size += dirSize($subFile);

}

}

}

closedir($dir_handle);

return $dir_size;

}

}

/*傳遞目前的目錄下的test目錄*/

$dir_size = dirSize('./test');

echo './test目錄檔案大小是:'.round($dir_size / pow(1024,1),2).'KB';

結果:

3、刪除目錄

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

/*

*刪除目錄

*@param string $dirName 目錄名

*/

function delDir($dirName)

{

/*php中的mkdir函數就可以建立目錄*/

if(file_exists($dirName))

{

if($dir_handle = @opendir($dirName))

{

while ($fileName = readdir($dir_handle))

{

/*排除兩個特殊目錄*/

if($fileName != '.' && $fileName != '..')

{

$subFile = $dirName.'/'.$fileName;

if(is_file($subFile))

{

unlink($subFile);

}

if(is_dir($subFile))

{

delDir($subFile);

}

}

}

closedir($dir_handle);

rmdir($dirName);

return $dirName.'目錄已經刪除';

}

}

}

/*傳遞test目錄的副本test1*/

echo delDir('./test1');

刪除成功的提示資訊

4、複製目錄

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

/*

*複製目錄

*@param string $dirSrc 原目錄名

*@param string $dirTo 目標目錄名

*/

function copyDir($dirSrc,$dirTo)

{

if(is_file($dirTo))

{

echo '目標目錄不能建立';/*目標不是一個*/

return;

}

if(!file_exists($dirTo))

{

/*目錄不存在則建立*/

mkdir($dirTo);

}

if($dir_handle = @opendir($dirSrc))

{

while ($fileName = readdir($dir_handle))

{

/*排除兩個特殊目錄*/

if($fileName != '.' && $fileName != '..')

{

$subSrcFile = $dirSrc.'/'.$fileName;

$subToFile = $dirTo.'/'.$fileName;

if(is_file($subSrcFile))

{

copy($subSrcFile,$subToFile);

}

if(is_dir($subSrcFile))

{

copyDir($subSrcFile,$subToFile);

}

}

}

closedir($dir_handle);

return $dirSrc.'目錄已經複製到'.$dirTo.'目錄';

}

}

echo copyDir('./test','../testcopy');

http://www.bkjia.com/PHPjc/886550.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/886550.htmlTechArticlePHP常用技術文之檔案操作和目錄操作總結 一、基本檔案的操作 檔案的基本操作有:檔案判斷、目錄判斷、檔案大小、讀寫性判斷、存在性...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.