PHP Common Technical text file operation and directory operation summary _php tutorial

Source: Internet
Author: User

Summary of file operation and directory operation in common technical text of PHP


First, the operation of the basic file

The basic operation of the file is: file Judgment, catalogue judgment, file size, reading and writing judgment, existence judgment and file time, etc.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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");

/*

* Declare a function, pass in the file name to get the files properties

* @param string $fileName file name

*/

function Getfilepro ($fileName)

{

if (!file_exists ($fileName))

{

Echo ' file does not exist
';

Return

}

/* is a normal file */

if (Is_file ($fileName))

{

echo $fileName. ' is a file
';

}

/* is the directory */

if (Is_dir ($fileName))

{

echo $fileName. ' is a directory ';

}

/* Type of output file */

The echo ' file type is: '. Getfiletype ($fileName). '
';

/* File size, conversion unit */

The echo ' File size is: '. GetFileSize (FileSize ($fileName)). '
';

/* File is readable */

if (is_readable ($fileName))

{

Echo ' File readable
';

}

/* File is writable */

if (is_writable ($fileName))

{

Echo ' file can be written
';

}

/* File is executable */

if (is_executable ($fileName))

{

Echo ' File executable
';

}

Echo ' File creation time: '. Date (' Y year M month J Day ', Filectime ($fileName)). '
';

Echo ' File last modified: '. Date (' Y year M-month J-Day ', Filemtime ($fileName)). '
';

Echo ' File last opened: '. Date (' Y year M-month J-Day ', Fileatime ($fileName)). '
';

}

/*

* Declare a function that returns the file type

* @param string $fileName file name

*/

function Getfiletype ($fileName)

{

$type = ";

Switch (filetype ($fileName))

{

Case ' file ': $type. = ' normal file '; break;

Case ' dir ': $type. = ' directory file ';

Case ' block ': $type. = ' block device file ';

Case ' char ': $type. = ' character device file ';

Case ' filo ': $type. = ' piping file ';

Case ' link ': $type. = ' symbolic link ';

Case ' unknown ': $type. = ' Unknown file ';

Default

}

return $type;

}

/*

* Declare a function that returns the file size

* @param int $bytes file byte number

*/

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;

}

/* Call the function and pass in the Test.txt file in the test directory */

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

?>

Results:

Second, the operation of the directory

Directory operations are: Traverse directory, delete, copy, size statistics, etc.

1. Traverse Directory

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

/*

* Traverse Directory

* @param string $dirName directory Name

*/

function Finddir ($dirName)

{

$num = 0; /* Count the number of sub-files */

$dir _handle = Opendir ($dirName); /* Open Directory */

/* Output Directory file */

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 "

'; '; < p=""> '; '; '; '; '; '; ';

Directory '. $dirName. ' Files

file name File Size File Type modification Time
'. $file. ''. FileSize ($dirFile). ''. FileType ($dirFile). ''. Date (' y/n/t ', Filemtime ($dirFile)). '
";

Closedir ($dir _handle); /* Close the directory */

echo "in ". $dirName. " Directory under ". $num. ' sub-file ';

}

/* Pass the test directory under the current directory */

Finddir ('./test ');

Results

2. Statistics Directory Size

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

/*

* Statistics Directory Size

* @param string $dirName directory Name

* @return Double

*/

function Dirsize ($dirName)

{

$dir _size = 0;

if ($dir _handle = @opendir ($dirName))

{

while ($fileName = Readdir ($dir _handle))

{

/* Exclude two special directories */

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;

}

}

/* Pass the test directory under the current directory */

$dir _size = Dirsize ('./test ');

The echo './test directory file size is: '. Round ($dir _size/pow (1024,1), 2). ' KB ';

Results:

3. Delete Directory

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

/*

* Delete Directory

* @param string $dirName directory Name

*/

function Deldir ($dirName)

{

The mkdir function in/*php can create a directory */

if (file_exists ($dirName))

{

if ($dir _handle = @opendir ($dirName))

{

while ($fileName = Readdir ($dir _handle))

{

/* Exclude two special directories */

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. ' Directory has been deleted ';

}

}

}

/* Pass a copy of the test directory test1*/

echo Deldir ('./test1 ');

Delete the prompt message for success

4. Copy Directory

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

/*

* Copy Directory

* @param string $DIRSRC The original directory name

* @param string $dirTo target directory name

*/

function Copydir ($DIRSRC, $dirTo)

{

if (Is_file ($dirTo))

{

Echo ' target directory cannot be created '; */target is not a */

Return

}

if (!file_exists ($dirTo))

{

/* Directory does not exist then create */

mkdir ($dirTo);

}

if ($dir _handle = @opendir ($DIRSRC))

{

while ($fileName = Readdir ($dir _handle))

{

/* Exclude two special directories */

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. ' Directory has been copied to '. $dirTo. ' Directory ';

}

}

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

http://www.bkjia.com/PHPjc/886550.html www.bkjia.com true http://www.bkjia.com/PHPjc/886550.html techarticle PHP Common Technical text file operation and directory operations summary of the basic files of the operation of the basic operation of the file: file judgment, directory judgment, file size, read and write judgment, existence of ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.