# Read/write to a csv file

**URL:** <https://www.phphelp.com/t/read-write-to-a-csv-file/36035>\
**Category:** Beginners - Learning PHP\
**Created:** [February 19, 2024, 1:48am UTC](https://www.phphelp.com/t/read-write-to-a-csv-file/36035 "2024-02-19T01:48:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hotspringsrus](https://www.phphelp.com/letter_avatar_proxy/v4/letter/h/d6d6ee/32.png) [@Hotspringsrus](https://www.phphelp.com/u/Hotspringsrus)\
**Post date:** [February 19, 2024, 1:48am UTC](https://www.phphelp.com/t/read-write-to-a-csv-file/36035/1 "2024-02-19T01:48:38Z")

</div>

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.

```auto
$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);

?>
```

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [February 19, 2024, 2:29am UTC](https://www.phphelp.com/t/read-write-to-a-csv-file/36035/2 "2024-02-19T02:29:39Z")

</div>

> [@Hotspringsrus](#):
>
> `fputcsv ($fnamenew, $newline);`

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.

---

<div class="post-metadata">

**Author:** ![Hotspringsrus](https://www.phphelp.com/letter_avatar_proxy/v4/letter/h/d6d6ee/32.png) [@Hotspringsrus](https://www.phphelp.com/u/Hotspringsrus)\
**Post date:** [February 19, 2024, 11:20pm UTC](https://www.phphelp.com/t/read-write-to-a-csv-file/36035/3 "2024-02-19T23:20:40Z")

</div>

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