Add data to csv file in specific columns using PHP

10,399

To do what you want to do, you should first break out your columns into an array exactly as they appear in your raw csv file:

$columns = ["sku","category","description","image","small_image","thumbnail", "price","color"];

Then you'll want to open the file and iterate over it, building an associative array of key-value pairs, checking whether or not a file exists for that image name, and if so, assigning the correct name to the array.

$rootDir = ""; //Root directory where your image files are located
$file = fopen("filehandle.csv", "r"); //Open the old file for reading
$newFile = fopen("newfilehandle.csv", "w"); //Create a new file for writing

while (($data = fgetcsv($file)) !== FALSE) {
    $row = array_combine($columns, $data);
    $filename = "{$row['sku']}.jpeg";
    if (file_exists("$rootDir/$filename")) {
        $row['image'] = $filename;
        $row['small_image'] = $filename;
        $row['thumbnail'] =  $filename; 
    }
    fputcsv($newFile, array_values($row)); //write data into new file
}

fclose($file);
fclose($newFile);

Now you have a copy of the original file with the new values inserted.

Share:
10,399
mlclm
Author by

mlclm

Updated on June 05, 2022

Comments

  • mlclm
    mlclm almost 2 years

    I have a csv file which has 14 columns, and hundreds of rows. There is a header which is "sku","category","description","brand" etc.

    All data is allready there but I'm trying to add some image filenames inside the CSV, in some specific columns ( "images", "small_image" , "thumnail" ).

    The header kind of looks like this:

    +-----+----------+-------------+-------+-------------+-----------+--------+------+
    | sku | category | description | image | small_image | thumbnail |  price | color|
    +-----+----------+-------------+-------+-------------+-----------+--------+------+
    

    so knowing how the header looks like, then the first row would be like:

    +-------+---------------+------------------+---+---+----+-----+--------+
    | sku01 | category name | description here |   |   |    | 99$ | black  |
    +-------+---------------+------------------+---+---+----+-----+--------+
    

    The filename for the jpeg is the same as the sku value on the same row , with the jpeg extension. So for example, if there is sku01 for the sku column on the first row, the filename would be sku01.jpeg in the image column on that same row. So would be the small_image value, and the thumbnail value.

    However the filename would only be specified IF the jpeg exist in a folder.

    So far I know how to use the fopen function to open the csv file, eventually store all data in an array ( I don't know if that would help in my case ) , then at some point use the fputcsv function after checking if file_exist in the specific folder on the server.

    But, to me it's still a big mess in my head as far as in what order I should use the functions. I 'm stuck. Also, I have no idea how to put the jpeg filename in the right columns ( image, thumbnail, small_image), because these columns are in the "middle" of the csv file, among other columns. They are not at the end of the csv file

    I would be really thankful to anyone who can put me on the right path.

  • mlclm
    mlclm about 9 years
    Thanks a lot for your help. I get the idea. I have no clue on how to put the values back in the CSV. Here is my code : pastebin.com/v1QQpaSv
  • mlclm
    mlclm about 9 years
    Thanks, however i'm still having problems, I'm probably doing something wrong because I get an error : Warning: array_combine() expects parameter 2 to be array, boolean given
  • kellanburket
    kellanburket about 9 years
    It's possible your csv files have an encoding issue. Do me a favor and goto terminal. Locate the directory where your file is stored and run file file.csv where 'file.csv' is the name of your file. Post the output here.
  • mlclm
    mlclm about 9 years
    thanks for your reply. it finally worked! There were a couple issues. You were right, when I checked in notepad++ it was in ANSI, I encoded it in UTF-8. Also $data['sku'] gave the following error : Undefined index: sku , I changed to $row['sku'] . The new csv file is created with the correct data inside. Thank you very much for your detailed reply and the follow-up, really appreciated , i'd like to send you a few $ via paypal to thank you because you really helped me out here, you can drop me a mail on mlclm (dot) design (at) gmail (dot) com to let me know your paypal address
  • kellanburket
    kellanburket about 9 years
    while I appreciate your offer, I'm more than happy to help, free of charge :)