Please click on the following address to download: CSV operation class
Instructions for use are as follows:
1. Generate a CSV file
Copy the Code code 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 number", "study number", "Student Name");
$csvfile = new Csvdatafile ("", ",", "w");
echo $csvfile->printline ($arr _export_titles);
Method One
$print _data1[] = 1;
$print _data1[] = "039413301";
$print _data1[] = "Zhang San";
echo $csvfile->printline ($print _data1);
$print _data2[] = 2;
$print _data2[] = "039413302";
$print _data2[] = "John Doe";
echo $csvfile->printline ($print _data2);
$print _data3[] = 3;
$print _data3[] = "039413303";
$print _data3[] = "Harry";
echo $csvfile->printline ($print _data3);
Method Two
$print _data[1][] = 1;
$print _data[1][] = "039413301";
$print _data[1][] = "Zhang San";
$print _data[2][] = 2;
$print _data[2][] = "039413302";
$print _data[2][] = "John Doe";
$print _data[3][] = 3;
$print _data[3][] = "039413303";
$print _data[3][] = "Harry";
echo $csvfile->printcsv ($print _data);
2. Open CSV Read data
Code
Copy the Code code 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 number, school number, student name") {
$pass = false;
}
if ($pass) {
$csv = new Csvdatafile ($filename);
while ($csv->next_row ()) {
$userid = Trim ($csv->f (' Student number '));
$classno = Trim ($csv->f (' study number '));
$username = Trim ($csv->f (' Student name '));
}
}
The above describes the Permeo security driver PHP CSV operation Class code, including the Permeo security driver aspects of the content, I hope that the PHP tutorial interested in a friend helpful.