Add data to csv file in specific columns using PHP

mlclm picture mlclm · Feb 16, 2015 · Viewed 9.1k times · Source

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.

Answer

kellanburket picture kellanburket · Feb 16, 2015

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.