Click Next to download: CSV operation class
The usage instructions are as follows:
1. Generate a CSV file
CopyCode The Code is as follows: require "./include/csvdatafile. php ";
Set_time_limit (200 );
Header ("Content-Type: Application/rfc822 ");
Header ('content-Disposition: attachment; filename=export.csv ');
$ Arr_export_titles = array ("student ID", "student ID", "Student name ");
$ Csvfile = new csvdatafile ("", "W ");
Echo $ csvfile-> printline ($ arr_export_titles );
// Method 1
$ Print_data1 [] = 1;
$ Print_datal [] = "039413301 ";
$ Print_data1 [] = "James ";
Echo $ csvfile-> printline ($ print_data1 );
$ Print_data2 [] = 2;
$ Print_data2 [] = "039413302 ";
$ Print_data2 [] = "Li Si ";
Echo $ csvfile-> printline ($ print_data2 );
$ Print_data3 [] = 3;
$ Print_data3 [] = "039413303 ";
$ Print_data3 [] = "Wang Wu ";
Echo $ csvfile-> printline ($ print_data3 );
// Method 2
$ Print_data [1] [] = 1;
$ Print_data [2] [] = "039413301 ";
$ Print_data [1] [] = "James ";
$ Print_data [2] [] = 2;
$ Print_data [2] [] = "039413302 ";
$ Print_data [2] [] = "Li Si ";
$ Print_data [3] [] = 3;
$ Print_data [3] [] = "039413303 ";
$ Print_data [3] [] = "Wang Wu ";
Echo $ csvfile-> printcsv ($ print_data );
2. Open CSV read data
CodeCopy codeThe Code is as follows: require "./include/csvdatafile. php ";
$ Filename = "E:/development/csvfile/datefile.csv ";
// Read File Source
$ Handle = fopen ($ filename, "R ");
$ Contents = fread ($ handle, filesize ($ filename ));
Fclose ($ handle );
// Format content for special chars
$ Contents = @ addslashes ($ contents );
$ Contents = @ str_replace ('\,', '\,', $ contents );
$ Contents = @ stripslashes ($ contents );
// Write to new file
$ Handle = @ fopen ($ filename, "W ");
@ Fwrite ($ handle, $ contents );
@ Fclose ($ handle );
$ FD = @ fopen ($ filename, "rb ");
$ First_line = str_replace (',', str_replace ('"','', trim (@ fgets ($ FD, 1000 ))));
@ Fclose ($ FD );
If ($ first_line! = "Student ID, student ID, Student name "){
$ Pass = false;
}
If ($ pass ){
$ CSV = new csvdatafile ($ filename );
While ($ CSV-> next_row ()){
$ Userid = trim ($ CSV-> F ('student number '));
$ Classno = trim ($ CSV-> F ('student ID '));
$ Username = trim ($ CSV-> F ('student name '));
}
}