Sanjoy Roy

[MCM, MCP, SCJP] – Senior PHP Programmer

Parse .CSV using PHP


CSVParser.php:

if ($fileType !='' && $fileType == 'text/comma-separated-values'){
		//parse csv file
		 $ext = 'csv';
		 $filename 	= $user.'.'.$section.'.upload.'.date('Ymd').'.'.$ext;
		 copy($_FILES['file']['tmp_name'], $UPLOAD_CSV_PATH.$filename);

		 $row 				= 1;
		$fhandle 			= @fopen($UPLOAD_CSV_PATH.$filename, "r");
		$columnHeaders		= '';
		$columnNames		= '';
		$rawData			= array();
		$count = 0;
		while (($fdata = fgetcsv($fhandle, 1000, $terminated_by, $enclosed_by)) !== FALSE) {
			$len = count($fdata)-1;
			if (strlen($fdata[$len])==0) array_pop($fdata);
			$rawData[$count] = $fdata;
			$count++;
		}
		fclose($fhandle);

		$newRowData = array();

		for($i=0; $iencode($rawData);

  }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: