simondowdles

PHP fgetcsv() parse issue - stackoverflow.com reference

Apr 7th, 2011
427
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.    
  3.     $current = dirname(__FILE__);
  4.  
  5.     //$con = mysqli_connect("xxxx", "xxxx", "xxxx");
  6.     //mysqli_select_db($con,"xxxxx");
  7.         //mysqli_set_charset($con, 'utf8');
  8.    
  9.     require_once("$current/../connection/connection.inc.php");
  10.     require_once("$current/./functions.php");
  11.  
  12.     $filename = "xxxxx.csv";
  13.     $log = "";
  14.     $log .= "LOG DATE: ".date('Y-m-d H:m:s')."<br/><hr/><br/>";
  15.     ini_set('max_execution_time', '3600'); //set to 1 hour for this script
  16.     ini_set('memory_limit', '128M'); //set max memory usage to 64M
  17.     ini_set('auto_detect_line_endings', true);
  18.     ini_set("display_errors", "off");
  19.     $log .= "SCRIPTS EXECUTION TIME SET TO: ".(ini_get("max_execution_time")/60)." mins<br/><br/>";
  20.  
  21.     $row = 0;
  22.         $items = 0;
  23.         $imgs = 0;
  24.     $data = Array();
  25.     $putdir = "$current/../product-images";
  26.     $fopen = fopen($filename, 'r');
  27.     $genderarray = Array("kids"=>"k", "kiddies"=>"k", "men"=>"g", "mens"=>"g", "male"=>"g", "gents"=>"g", "ladies"=>"l", "lady"=>"l", "womens"=>"l", "female"=>"l", "unisex"=>"u");
  28.         $ouncetoml = 29.5735296; //1 oz = 29.5735296 ml
  29.  
  30.     $log .= "PERFORMING CURRENCY CONVERSION OPTIONS:<br/><br/>";
  31.     $stockarray = array(); //push all product items into this array to do stock comparison...
  32.     $er = 0.14563; //default fallback exchange rate
  33.     $xr_feed ="http://themoneyconverter.com/ZAR/rss.xml";
  34.     $log .= "Default fallback exchange rate is 1 USD = $er ZAR<br/>Exchange Rate XML Feed URL is $xr_feed<br/>";
  35.     $feed = file_get_contents($xr_feed);
  36.         if($feed):
  37.         $xml = new SimpleXMLElement($feed);
  38.         if($xml->channel->item[57]->title == "USD/ZAR"):
  39.             $er = $xml->channel->item[57]->description;
  40.             $er = split("=",$er);
  41.             $er = trim($er[1]);
  42.             $er = split(" ",$er);
  43.             $er = trim($er[0]);
  44.             $log .= "SUCCESS Got current exchange rate, 1USD = $er ZAR<br/>";
  45.         else:
  46.             $log .= "FAIL Cannot determine current conversion rate, falling back to 1 USD = $er ZAR<br/>";
  47.         endif;
  48.     else:
  49.         $log .= "FAIL Cannot open XML exchange rate feed, falling back to 1 USD = $er ZAR<br/>";
  50.     endif;
  51.  
  52.     $log .= "<br/>STARTING IMAGE & DATA COPY FROM SERVER TO SERVER:<br/><br/>";
  53.  
  54.     while(($data = fgetcsv($fopen, 0, ",")) !== false):
  55.         $row++;
  56.         if($row > 1): //initial limit set to 10 000 items otherwise we'll chew up the server & spit it out...
  57.             $num = count($data);
  58.             $code = $data[0];
  59.             $brand = $data[1];
  60.             $designer = $data[2];
  61.             $size = $data[3];
  62.            
  63.                         //SIZE CONVERSION
  64.                         $size = explode(" ",trim($size));
  65.                         $unit = strtolower(trim($size[1]));
  66.                         switch($unit):
  67.                             case 'oz':
  68.                                 $size = trim($size[0]);
  69.                                 $size = $size*$ouncetoml;
  70.                                 $size = round(ceil($size/5)*5);
  71.                                 $size = $size." ml";
  72.                             break;
  73.                             case 'ml':
  74.                                 $size = trim($size[0]);
  75.                                 $size = round(ceil($size/5)*5);
  76.                                 $size = $size." ml";
  77.                             break;
  78.                         endswitch;
  79.                         if (is_array($size)):
  80.                             $size = "n/a";
  81.                         endif;
  82.                         //END SIZE CONVERSION
  83.                        
  84.             $type = $data[4];
  85.             $gender = strtolower(trim($data[5]));
  86.             $gender = $genderarray[$gender];
  87.             $set = $data[6];
  88.             $description = $data[7];
  89.            
  90.                 //COST PRICE, CONVERSION & FORMULA WORK NOW
  91.                 $cost = $data[9];
  92.                 $cost = $cost/$er;
  93.                 $cost = round($cost,2);
  94.                 $oprice = floor($cost)*2.15;
  95.                 $oprice = $oprice+60;
  96.                 $oprice = round($oprice,2);
  97.                 //END COST PRICE CONVERSIONS NOW
  98.                
  99.             $image = $data[8];
  100.             $ext1 = explode(".",$image);
  101.             $ext2 = array_slice($ext1,-1);
  102.             $ext = $ext2[0]; //file extension
  103.             $imagename = strtolower($data[0]);
  104.             array_push($stockarray, $code);
  105.                         //Insert data into table
  106.  
  107.                         $isql = "INSERT INTO tblfragrances (fgender, fbrand, ftitle, fstylenumber, fimage, fdescription, fsubcategory, fsize, fcost, foprice, fremote) VALUES ('$gender', '$designer', '$designer - $brand', '$code', '$imagename', \"$description\", '$type', '$size', '$cost', '$oprice', '1') ON DUPLICATE KEY UPDATE fgender='$gender', fbrand='$designer', ftitle='$designer - $brand', fstylenumber='$code', fimage='$imagename', fdescription=\"$description\", fsubcategory='$type', fsize='$size', fcost='$cost', foprice='$oprice', fremote='1'";
  108.                         $additem = mysqli_query($con, $isql);
  109.                         if(!$additem):
  110.                             $log .= "ERROR Adding item $code to database<br/>";
  111.                         else:
  112.                             $log .= "ADDED Item $code to database<br/>";
  113.                             $items++;
  114.                         endif;
  115.            
  116.             if(file_exists("$putdir/$imagename.$ext") && filesize("$putdir/$imagename.$ext") > 5):
  117.                 $log .= "SKIPPING EXISTS $image > $putdir/$imagename.$ext<br/>";
  118.                 resize("$putdir/$imagename.$ext", "$putdir/200/$imagename.$ext", 200, 200, 80);
  119.                 resize("$putdir/$imagename.$ext", "$putdir/400/$imagename.$ext", 400, 400, 80);
  120.             else:          
  121.                 $content = file_get_contents($image);
  122.                 if(!$content):
  123.                     $log .= "ERROR GETTING FILE $image<br/>";
  124.                 else:
  125.                     $fp = fopen("$putdir/$imagename.$ext", "w");
  126.                     $addimage = fwrite($fp, $content);
  127.                     fclose($fp);
  128.                     $log .= ($addimage == true ? "SUCCESS $image > $putdir/$imagename.$ext<br/>" : "FAIL $image > $putdir/$imagename.$ext<br/>");
  129.                                         ($addimage == true ? $imgs++ : null);
  130.                     if(!resize("$putdir/$imagename.$ext", "$putdir/200/$imagename.$ext", 200, 200, 80)): $log .= "Error making 200 thumb<br/>"; endif;
  131.                     if(!resize("$putdir/$imagename.$ext", "$putdir/400/$imagename.$ext", 400, 400, 80)): $log .= "Error making 400 image<br/>"; endif;
  132.                 endif;
  133.             endif;
  134.         endif;
  135.     endwhile;
  136.    
  137.     //now we create a string of all items that were looped over in the CSV file
  138.     $count = count($stockarray);
  139.     $instock = implode("','", $stockarray);
  140.     //now that we have a string representation of all stock, we can delete all in the DB that does not reside in this string, simple huh
  141.     $dsql = "DELETE FROM tblfragrances WHERE fstylenumber NOT IN ('$instock') AND fremote = '1'";
  142.     $delete = mysqli_query($con,$dsql);
  143.     $log .= ($delete !== false ? "<br/>SUCCESS Removed all items in database that were not in CSV file<br/>" : "<br/>ERROR Removing all items in database that were not in CSV file<br/>");
  144.     fclose($fopen);
  145.     $log .= "<br/>$count ITEMS IN ARRAY, $row ROWS IN CSV FILE<br/>";
  146.     $log .= "<br/>ADDED / UPDATED $items PRODUCTS TO ETERNITI DATABASE<br/>";
  147.     $log .= "<br/>TRANSFERRED $imgs IMAGES FROM REMOTE SERVER TO LOCAL ETERNITI SERVER FOR RESIZING.<br/>";
  148.     $log .= "<br/>LAST LINE VALUE IS $code<br/>";
  149.     $headers = "From: xxxxx<xxxxx>\r\n";
  150.     $headers .= "Content-Type: text/html; Charset: UTF-8\r\n"; 
  151.     mail("xxxxx", "xxxxx", $log, $headers);
  152.    
  153. ?>
RAW Paste Data