PHP implementation of CSV file import and Export class _php tutorial

Source: Internet
Author: User
Tags export class

PHP implementation of CSV file import and Export classes


This article mainly introduces the PHP implementation of CSV file import and export classes, examples of PHP for CSV file operation skills, very practical value, the need for friends can refer to the following

This article describes the import and export classes of the PHP implementation csv file. Share to everyone for your reference. Specific as follows:

?

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

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

Assembly data

foreach ($param _arr as $k = + $v) {

foreach ($v as $k 1=> $v 1) {

$export _str. = Implode (', ', $v 1). " n ";

}

}

Export the $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 = ' not processed ';

$filesize = 1; 1MB

$maxsize = $filesize * 1024 * 1024;

$max _column = 1000;

Detects if a file exists

if ($flag = = = Flase) {

if (!file_exists ($path)) {

$msg = ' file does not exist ';

$flag = true;

}

}

Detecting file formats

if ($flag = = = Flase) {

$ext = Preg_replace ("/.*." ( [^.] +)/"," $ ", $path);

if ($ext! = ' csv ') {

$msg = ' Import only csv format file ';

$flag = true;

}

}

Detecting File size

if ($flag = = = Flase) {

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

$msg = ' The imported file must not exceed '. $maxsize. ' b file ';

$flag = true;

}

}

Read 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 = ' file does not conform to specifications true to have: '. $num. ' Column data ';

$flag = true;

Break

}

if ($flag = = = Flase) {

for ($i =0; $i <3; $i + +) {

if ($row = = 0) {

Break

}

Build data

$dataArray [$row] [$i] = $data [$i];

}

}

$row + +;

}

}

return $dataArray;

}

}

$param _arr = Array (

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

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

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

2=>array (' Xiaohai3 ', ' 123456 ', ' 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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/973118.html www.bkjia.com true http://www.bkjia.com/PHPjc/973118.html techarticle PHP Implementation of CSV file import and export class This article mainly introduces the PHP implementation of CSV file import and export classes, the example of PHP for CSV file operation skills, very 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.