Update the record in all csv file

There are 50 csv files. each csv file have the same column’s name. some content in the column are the same, some don’t.

eg:

test1.csv,  example1.csv, hello.csv,  world.csv.......test70.csv. 

now, i want to make a change to two column’s content.

a, all csv files have a column named qty. whose rows content are all 0. now, i want to change it into 888

b, all csv files have a column named img. whose rows content are all

 upload/img1.jpg
 upload/img2.jpg
 upload/img3.jpg
 upload/img01.jpg
 upload/img14.jpg
 upload/img01.jpg
.......

If i open each csv file then do a search and replace. i fell it too bored.

Thank you in advance.

the csv file:

http://phplist.xxmn.com/csv.jpg

i want to delete Upload in the image column, change 0 to 888 in the qty column.

the csv file. http://phplist.xxmn.com/women.csv

Take a look these two articles, they may help you to get started doing the update on your own:

http://www.homeandlearn.co.uk/php/php10p6.html
http://alpho011.hubpages.com/hub/PHP-form-inserts-data-into-CSV-file

The first article shows you how to retrieve the data from the csv file. Once you got the data, you simply update each line according to your requirement and then write it back to the file. Note the example in second article is inserting or adding the data back into the file but you would overwrite your file in your case.

If you’re having trouble doing the update part then post the data you got from one of your files here and we will suggest a method to update them.

Edit: I didn’t see that you have already posted a file. Let us know if you still having trouble doing the update after reading those articles.

<?php
$dir    = getcwd();
$files = array_diff(scandir($dir), array('..', '.','change.php'));
foreach ($files as $file) {
    if (($handle = fopen($file, "r")) !== FALSE) {
        $new_content = '';
        while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) {
            //var_dump($data);
           $data[2]=888; 
            $new_content .= implode(',', $data);
            //var_dump($new_content);
        }
       file_put_contents($file, $new_content);
        echo 'down';
    }
}

?>

this is my code. why it can’t work thank u.the csv file. http://phplist.xxmn.com/women.csv

Try it with this code and see if the results are what you’re looking for:

<?php
   $dir   = getcwd();
   $files = scandir($dir);
   foreach ($files as $file)
   {
	   $parts = pathinfo($file);
	   if($parts['extension']!="csv") continue;
	   if (($handle = fopen($file, "r")) !== FALSE)
	   {
		   $new_content = '';
		   while (($data = fgetcsv($handle, 100000, ",")) !== FALSE)
		   {
			   $data[2]=888; 
			   $new_content .= implode(',', $data)."\
";
		   }
		   fclose($handle);
		   file_put_contents($file, $new_content);
	   }
   }
?>

Note that the files to be updated must have csv file extension; also the first line (title) will also get the update.

I took a second look and see why yours did not work - the end of line character (
) is missing. Try that and see if it works with the change.

i put your code in, it changed the header. how to exclude the header?

Here is one way:

<?php
   $dir   = getcwd();
   $files = scandir($dir);
   foreach ($files as $file)
   {
	   $parts = pathinfo($file);
	   if($parts['extension']!="csv") continue;
	   if (($handle = fopen($file, "r")) !== FALSE)
	   {
		   $new_content =  implode(',', fgetcsv($handle, 100000, ","))."\
";
		   while (($data = fgetcsv($handle, 100000, ",")) !== FALSE)
		   {
			   $data[2]=888;
			   $new_content .= implode(',', $data)."\
";
		   }
		   fclose($handle);
		   file_put_contents($file, $new_content);
	   }
   }
?>

i am sorry. it still can’t work. if you test the code to phplist.xxmn.com/women.csv this csv file. you will be find. the csv content are all disorderd

You’re showing me a file that is entirely different than the one in discussion. :confused:

http://phplist.xxmn.com/women.csv you can test it. thank you

Your original file has the following:

store,image,qty,min_qty,use_config_min_qty,is_qty_decimal,backorders,use_config_backorders,min_sale_qty,use_config_min_sale_qty,max_sale_qty,use_config_max_sale_qty,is_in_stock

While the last one has this:

store,websites,attribute_set,type,category_ids,sku,has_options,name,url_key,gift_message_available,meta_title,meta_description,image,small_image,thumbnail,gallery,options_container,page_layout,image_label,small_image_label,thumbnail_label,url_path,price,special_price,cost,weight,status,tax_class_id,visibility,enable_googlecheckout,is_recurring,is_imported,description,short_description,meta_keyword,custom_layout_update,special_from_date,special_to_date,news_from_date,news_to_date,custom_design_from,custom_design_to,qty,min_qty,use_config_min_qty,is_qty_decimal,backorders,use_config_backorders,min_sale_qty,use_config_min_sale_qty,max_sale_qty,use_config_max_sale_qty,is_in_stock,low_stock_date,notify_stock_qty,use_config_notify_stock_qty,manage_stock,use_config_manage_stock,stock_status_changed_automatically,use_config_qty_increments,qty_increments,use_config_enable_qty_increments,enable_qty_increments,product_name,store_id,product_type_id,product_status_changed,product_changed_websites

You want to update qty ($data[2]), but the column for the last one is attribute_set. Those two files are not the same.