-1

So I've looked at fputcsv but it seems to replace the whole file. Is there a way that a I can replace or edit a particular row in a CSV file using PHP? I do NOT want to upload (as an array or some other data structure) the entire CSV edit the upload and then replace or overwrite the file. I want to edit the CSV file in place.

For example if I had example.csv that looks like:

Name  Amount Price
Chair 500    20.00
Boat  20     20000.00

And I wanted to replace the amount of chair's to 100. How would I go about that by just editing the CSV file? Can I do it without uploading the entire file. I'm avoiding that because I'm thinking this is going to be a rather large file and changes may occur frequently.

Also I am saying upload this might be poorly phrased as this is all done on the server side not the client.

Bren
  • 3,516
  • 11
  • 41
  • 73
  • a simple str_replace() may work, but we don't have enough details –  Jan 22 '15 at 20:36
  • It depends, whether you want to change n bytes of data to n bytes of data. I this case you can modify the file easily. If you want to delete or insert the data into the middle of the file, you'll need to rewrite it. – user4035 Jan 22 '15 at 20:42
  • @Dagon I hope my edits make things more clear – Bren Jan 22 '15 at 20:48
  • Why don't you want to use a database? SQLite for example? – user4035 Jan 22 '15 at 20:52
  • can you just replace "Chair 500 20.00" with another line, or do you need to find "Chair" ? –  Jan 22 '15 at 20:52
  • @Dagon I could replace – Bren Jan 22 '15 at 20:53
  • See answer #4 in http://forums.phpfreaks.com/topic/231226-modify-a-value-in-csv-file/ might be of help. But a DB would be so much easier to use. You can also import CSV into DB. – Funk Forty Niner Jan 22 '15 at 20:55
  • ^^i was going to write that up but with file_get_contents(), but i wont bother now. thanks @Fred-ii- –  Jan 22 '15 at 20:56
  • @Dagon Be my guest ;-) I say go for it. You have my blessing ;-) Make it fast though. One more vote and it's done for. – Funk Forty Niner Jan 22 '15 at 20:56
  • 1
    well if you insist oh wise one. –  Jan 22 '15 at 21:00
  • 1
    Personally, if I had to deal with CSV files (again), I would use Excel and simply upload the update. Open Office also has a related application you can use if you don't have Excel. However, and again; using a DB would simplify things a lot more. If DB isn't an option because you may fear its complexity at first, then don't let it intimidate you. I use to work with files like that before I took the initiative to learn SQL. – Funk Forty Niner Jan 22 '15 at 21:08

2 Answers2

5

my option:

$f="file.csv";

$file=file_get_contents($f);
$file = str_replace("Chair 500", "Chair 100", $file);


file_put_contents($f, $file);

“Danger, Will Robinson!” don't try with file larger than available memory

1

CSV, for all that it contains "formatted" data, is still just a flat text file. If what you're replacing in the file is a different size than what's there, e.g. your 10 -> 100, you will have to re-write the file to make space for the extra characters. You can't just find where the 10 is and start writing, because that'll start overwriting whatever came AFTER the 10, e.g.

foo;10;bar      <--original
foo;100bar      <--blind string replacement

Now it's not a 3-field CSV record anymore. It's a two-field record. Similarly, for 10 -> 2

foo;10;bar
foo;20;bar

10 -> 2 rips out two characters, writes in one character, and leaves the original 2nd character in place. Now you've doubled your counter instead of reducing it.

Basically you'd have to do this:

1) open new temp file
2) copy records from original CSV until you reach the record you want
3) read record into client-side
4) decode record into native data structure
5) modify data
6) write modified record out as csv to temp file
7) copy the remainder of the original file
8) delete original
9) rename temp -> original
Marc B
  • 356,200
  • 43
  • 426
  • 500