PHP to import and export CSV files _ PHP Tutorial

Source: Internet
Author: User
Tags export class
PHP imports and exports CSV files. PHP-based CSV file import and export class this article mainly introduces PHP-based CSV file import and export class. The example shows php's operation skills on csv files, very practical value PHP implements CSV file import and export class

This article mainly introduces how to import and export CSV files in PHP. The example analyzes the php operation skills for csv files, which is very useful. For more information, see

This example describes how to import and export CSV files in PHP. Share it with you for your reference. The details are as follows:

?

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

107

108

109

110

111

112

113

/**

* CSV file processing class

*/

Class Csv {

Public $ csv_array; // csv array data

Public $ csv_str; // csv file data

Public function _ construct ($ param_arr, $ column ){

$ This-> csv_array = $ param_arr;

$ This-> path = $ path;

$ This-> column = $ column;

}

/**

* Export

**/

Public function export (){

If (empty ($ this-> csv_array) | empty ($ this-> column )){

Return false;

}

$ Param_arr = $ this-> csv_array;

Unset ($ this-> csv_array );

$ Export_str = implode (',', $ param_arr ['Nav']). "n ";

Unset ($ param_arr ['Nav']);

// Assemble data

Foreach ($ param_arr as $ k => $ v ){

Foreach ($ v as $ k1 => $ v1 ){

$ Export_str. = implode (',', $ v1). "n ";

}

}

// Export $ export_str

Header ("Cache-Control: public ");

Header ("Pragma: public ");

Header ("Content-type: application/vnd. ms-excel ");

Header ("Content-Disposition: attachment?filename=txxx.csv ");

Header ('content-Type: APPLICATION/OCTET-STREAM ');

Ob_start ();

// $ File_str = iconv ("UTF-8", 'gbk', $ export_str );

Ob_end_clean ();

Echo $ export_str;

}

/**

* Import

**/

Public function import ($ path, $ column = 3 ){

$ Flag = flase;

$ Code = 0;

$ Msg = 'unprocessed ';

$ Filesize = 1; // 1 MB

$ Maxsize = $ filesize * 1024*1024;

$ Max_column = 1000;

// Check whether the file exists

If ($ flag === flase ){

If (! File_exists ($ path )){

$ Msg = 'File does not exist ';

$ Flag = true;

}

}

// Check the file format

If ($ flag === flase ){

$ Ext = preg_replace ("/. *. ([^.] +)/", "$1", $ path );

If ($ ext! = 'Csv '){

$ Msg = 'only CSV files can be imported ';

$ Flag = true;

}

}

// Check the file size

If ($ flag === flase ){

If (filesize ($ path)> $ maxsize ){

$ Msg = 'Imported file cannot exceed '. $ maxsize.' B file ';

$ Flag = true;

}

}

// Read the file

If ($ flag = flase ){

$ Row = 0;

$ Handle = fopen ($ path, 'r ');

$ DataArray = array ();

While ($ data = fgetcsv ($ handle, $ max_column ,",")){

$ Num = count ($ data );

If ($ num <$ column ){

$ Msg = 'The file does not meet the actual specifications: '. $ num.' column data ';

$ Flag = true;

Break;

}

If ($ flag === flase ){

For ($ I = 0; $ I <3; $ I ++ ){

If ($ row = 0 ){

Break;

}

// Set up data

$ DataArray [$ row] [$ I] = $ data [$ I];

}

}

$ Row ++;

}

}

Return $ dataArray;

}

}

$ Param_arr = array (

'Nav' => array ('username', 'password', 'mailbox '),

Array (0 => array ('xiaohai1', '20140901', 'xiaohai1 @ zhongsou.com '),

1 => array ('xiaohai2 ', '000000', 'xiaohai2 @ zhongsou.com '),

2 => array ('xiaohai3 ', '123', 'xiaohai3 @ zhongsou.com ')

));

$ Column = 3;

$ Csv = new Csv ($ param_arr, $ column );

// $ Csv-> export ();

$ Path = 'C: \ Documents and Settings \ Administrator \ Temp \ txxx.csv ';

$ Import_arr = $ csv-> import ($ path, 3 );

Var_dump ($ import_arr );

?>

I hope this article will help you with php programming.

This article describes how to import and export CSV files in PHP. The example analyzes the php operation skills for csv files, which is of great practical value...

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.