Revision 1348 is a pre-publication revision. (Viewing current revision instead.)

PHP – Add data to CSV file in specific columns

Base on the requirement of my client, I have to create a CSV file which has a lot of columns, and hundreds of rows. The header looks like this: SKU | category | description | image | small_image | thumbnail | price | color I need to read the data from other files and put the suitable data in the correct column. I tried many different ways but it didn't work. Finally, I found out a way to do it quickly, here is how I did with PHP: At first, I break out the columns into an array precisely as they appear in the raw CSV file:
$columns = ["sku","category","description","image","small_image","thumbnail", "price","color"];
Then I 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 the 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);
Hope it is useful to you 🙂

Revisions

Revision Differences

January 2, 2018 @ 18:11:28Current Revision
Title
Deleted: PHP - Add data to SCV file in specific columns Added: PHP - Add data to CSV file in specific columns
Content
Deleted: <p> 
Deleted: I have a CSV file which has 14 columns, and hundreds of rows. There is a header which is &quot;SKU&quot; ,&quot;category&quot;,&quot; description&quot; ,&quot;brand&quot; etc. 
Deleted: </p> 
Deleted: <p> 
Deleted: All data is already there but I&#39;m trying to add some image filenames inside the CSV, in some specific columns ( &quot;images&quot;, &quot;small_image&quot;, &quot;thumbnail&quot; ). 
Deleted: </p> 
Deleted: <p> 
 Added: Base on the requirement of my client, I have to create a CSV file which has a lot of columns, and hundreds of rows.
Deleted: The header kind of looks like this: Added: The header looks like this:
Deleted: </p> 
Deleted: <p> 
Deleted: SKU | category | description | image | small_image | thumbnail | price | color Added: SKU | category | description | image | small_image | thumbnail | price | color
Deleted: </p> 
Deleted: <p> 
Deleted: so knowing how the header looks like, then the first row would be like sku01 | category name whatever | description here | | | | 99$ | black 
Deleted: </p> 
Deleted: <p> 
Deleted: The filename for the jpeg is the same as the &quot;SKU&quot; value on the same row, with the jpeg extension. So for example, if there is &quot;sku01&quot; 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. 
Deleted: </p> 
Deleted: <p> 
Deleted: <strong>However, the filename would only be specified IF the jpeg exists in a folder.</strong> 
Deleted: </p> 
Deleted: <p> 
 Added: I need to read the data from other files and put the suitable data in the correct column.
 Added: I tried many different ways but it didn't work. Finally, I found out a way to do it quickly, here is how I did with PHP:
 Added: At first, I break out the columns into an array precisely as they appear in the raw CSV file:
 Added: <pre class="lang-php prettyprint prettyprinted" ><code><span class="pln">$columns </span><span class="pun">=</span> <span class="pun">[</span><span class="str">" sku"</span><span class="pun">,</span><span class="str">" category"</span><span class="pun">,</span><span class="str">" description"</span><span class="pun">,</span><span class="str">" image"</span><span class="pun">,</span><span class="str">" small_image"</span><span class="pun">,</span><span class="str">" thumbnail"</span><span class="pun">,</span> <span class="str">" price"</span><span class="pun">,</span><span class="str">" color"</span><span class="pun">]; </span></code></pre>
Deleted: So far I know how to use the fopen function to open the CSV file, eventually store all data in an array ( I don&#39;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.Added: Then I 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.
Deleted: </p> 
Deleted: <p> 
Deleted: But, to me, it&#39;s still a big mess in my head as far as in what order I should use the functions. I &#39;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 &quot;middle&quot; of the CSV file, among other columns. They are not at the end of the CSV file 
Deleted: </p> 
 Added: <pre class="lang-php prettyprint prettyprinted" ><code><span class="pln">$rootDir </span><span class="pun">=</span> <span class="str">" "</span><span class="pun">;</span> <span class="com">//Root directory where the image files are located</span><span class="pln">
 Added: $file </span><span class="pun">= </span><span class="pln"> fopen</span><span class="pun">( </span><span class="str">" filehandle.csv" </span><span class="pun">,</span> <span class="str">" r"</span><span class="pun">);</span> <span class="com">//Open the old file for reading</span><span class="pln">
 Added: $newFile </span><span class="pun">= </span><span class="pln"> fopen</span><span class="pun">( </span><span class="str">" newfilehandle.csv" </span><span class="pun">,</span> <span class="str">" w"</span><span class="pun">);</span> <span class="com">//Create a new file for writing</span>
 Added: <span class="kwd">while</span> <span class="pun">( (</span><span class="pln">$data </span><span class="pun">= </span><span class="pln"> fgetcsv</span><span class="pun">( </span><span class="pln">$file</span><span class="pun">))</span> <span class="pun">!= =</span><span class="pln"> FALSE</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
 Added: $row </span><span class="pun">= </span><span class="pln"> array_combine</span><span class="pun">( </span><span class="pln">$columns</span><span class="pun">,</span><span class="pln"> $data</span><span class="pun">) ;</span><span class="pln">
 Added: $filename </span><span class="pun">=</span> <span class="str">" {$row['sku']}.jpeg" </span><span class="pun">;</span>
 Added: <span class="kwd">if</span> <span class="pun">( </span><span class="pln">file_ exists</span><span class="pun">( </span><span class="str">" $rootDir/$filename" </span><span class="pun">))</span> <span class="pun">{</span><span class="pln">
 Added: $row</span><span class="pun">[</span><span class="str">'image'</span><span class="pun">]</span> <span class="pun">= </span><span class="pln"> $filename</span><span class="pun">; </span><span class="pln">
 Added: $row</span><span class="pun">[</span><span class="str">'small_ image'</span><span class="pun">]</span> <span class="pun">= </span><span class="pln"> $filename</span><span class="pun">; </span><span class="pln">
 Added: $row</span><span class="pun">[</span><span class="str">'thumbnail'</span><span class="pun">]</span> <span class="pun">= </span><span class="pln"> $filename</span><span class="pun">;</span>
 Added: <span class="pun">}</span><span class="pln">
 Added: fputcsv</span><span class="pun">( </span><span class="pln">$newFile</span><span class="pun">,</span><span class="pln"> array_values</span><span class="pun">( </span><span class="pln">$row</span><span class="pun">));</span> <span class="com">//Write data into new file</span>
 Added: <span class="pun">}</span><span class="pln">
 Added: fclose</span><span class="pun">( </span><span class="pln">$file</span><span class="pun">) ;</span><span class="pln">
 Added: fclose</span><span class="pun">( </span><span class="pln">$newFile</span><span class="pun">) ;</span></code></pre>
 Added: Hope it is useful to you 🙂

Note: Spaces may be added to comparison text to allow better line wrapping.

No comments yet.

Leave a Reply