Read/write to a csv file

I am trying to read a couple column values of a spreadsheet (13 columns X 4000 rows) saved as a csv file. I believe my code is correctly looking to process the csv file by rows. When I run the code the parser appears to be looking past the data as it errors looking for key 14 and above. I want to read the filename to append/rename and then put the last edit date into a new csv or replace. I found some issues in the csv file as it was adding extra data when finding characters like commas and double quotes but I removed them.

$fname="index_csv.csv";
$fnamenew="indexnew.csv";
$xhandle= fopen ($fnamenew, "a");

if (($handle=fopen ($fname, "r")) !==FALSE){
	while(($line =  fgetcsv($handle)) !== FALSE) {
 		list($totstring, $totlen, $period, $http, $lenanna, $lentitle, $post, $title, $fnamelen, $old, $oldlessext, $bat, $fcdate) = $line;
		if(file_exists($old)){
			$cfiledate= date('y-m-d',filectime($old));
			
			$new=$post . "_". $old; // this adds the post number to the beginning of the file name.
			
			
			echo "Now processing: " . $new . "<br>";
			rename ($old, $new);
			
			$newline = array($totstring, $totlen,  $period,  $http ,  $lenanna , $lentitle, $post, $title, $fnamelen, $old, $oldlessex, $bat,  $cfiledate);

			fputcsv ($fnamenew, $newline);
			unlink($old);
		}
	}
   }
fclose($handle);

fclose($xhandle);

?>

The fputcsv() uses the fopen() file handle, $xhandle, not the filename. This should have been producing a php error.

As to any operational problems, you would need to post example data and the corresponding php errors that you got.

That was the fix. Since the process didn’t release the file, I had issues uploading the updated version of my php. Thank you.

Sponsor our Newsletter | Privacy Policy | Terms of Service