Guest User

image_download

a guest
Feb 6th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.86 KB | None | 0 0
  1. <?php
  2.  
  3. $servername = "docker";
  4. $username = "docker";
  5. $password = "docker";
  6.  
  7. try {
  8.     $db = new PDO("mysql:host=$servername;dbname=docker", $username, $password);
  9.     // set the PDO error mode to exception
  10.     $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11.     echo "Connected successfully";
  12.     }
  13. catch(PDOException $e)
  14.     {
  15.     echo "Connection failed: " . $e->getMessage();
  16.     }
  17.  
  18. $import_products = true;
  19. $import_products_to_categories = false;
  20.  
  21.  
  22. if ($import_products == true) {
  23.  
  24.     $handle = fopen('products.csv', "r");
  25.  
  26.     $rowno = 0;
  27.     while (($data = fgetcsv($handle, 9000, ",")) !== FALSE) {
  28.  
  29.         $num = count($data);
  30.         $rowno++;
  31.  
  32.         if($rowno == 1) {
  33.             print_r($data);
  34.         }
  35.  
  36.         if($rowno > 1) {
  37.             $product_id = $data[0];
  38.             $product_model = $data[1];
  39.             $product_name = $data[2];
  40.             $product_summary = $data[3];
  41.             $product_description = $data[4];
  42.             $product_url = $data[5];
  43.             $manID = $data[6];
  44.             $product_status = $data[7];
  45.             $vatcode = $data[8];
  46.             $RRPprice = $data[9];
  47.             $minQTY = $data[10];
  48.             $weight = $data[11];
  49.             $meta_title = $data[12];
  50.             $meta_decs = $data[13];
  51.             $meta_keywords = $data[14];
  52.             $visibility = $data[15];
  53.             $createDate = $data[16];
  54.             $lastModified = $data[17];
  55.             $featured = $data[18];
  56.             $promoted = $data[19];
  57.             $quantity = $data[20];
  58.             $ship_adj = $data[21];
  59.             $custom1 = $data[22];
  60.             $custom2 = $data[23];
  61.             $custom3 = $data[24];
  62.             $custom4 = $data[25];
  63.             $custom5 = $data[26];
  64.             $custom6 = $data[27];
  65.             $custom7 = $data[28];
  66.             $custom8 = $data[29];
  67.             $custom9 = $data[30];
  68.             $custom10 = $data[31];
  69.             $customText1 = $data[32];
  70.             $customText2 = $data[33];
  71.             $customText3 = $data[34];
  72.             $topTenRanking = $data[35];
  73.             $shippingGroup = $data[36];
  74.  
  75.  
  76.             $domain = 'http://www.tvandfilmsupplies.co.uk/';
  77.             $prodPath = 'ecommerce/product/';
  78.             $img_path = '/var/www/platform/public/images';
  79.  
  80.             $images = array();
  81.             $imageSrc = getElementByIdAsString($domain  . $prodPath . $product_url . '.aspx', 'ctl00_cphContent1_ctl00_ProductImageViewer1_imgFull');
  82.  
  83.             if ($imageSrc) {
  84.                 $name = basename($imageSrc);
  85.  
  86.                 $image = $img_path . '/' . $name;
  87.                
  88.                 $imageSave = file_put_contents($image, file_get_contents($domain . $imageSrc));
  89.  
  90.                 if($imageSave) {
  91.                     // Update product image
  92.                     $imageUpdate = $db->prepare('UPDATE products SET products_image = :image_name WHERE products_id = :products_id');
  93.                     $imageUpdate->execute(array(':image_name' => $name, ':products_id' => $product_id));
  94.                 }
  95.  
  96.             }
  97.  
  98.         }
  99.  
  100.     }
  101.     fclose($handle);
  102. }
  103.  
  104.  
  105. /**
  106.  * Downloads a web page from $url, selects the the element by $id
  107.  * and returns it's xml string representation.
  108.  */
  109. function getElementByIdAsString($url, $id, $pretty = true) {
  110.     $doc = new DOMDocument();
  111.     @$doc->loadHTMLFile($url);
  112.  
  113.     if(!$doc) {
  114.         throw new Exception("Failed to load $url");
  115.     }
  116.  
  117.     // Obtain the element
  118.     $element = $doc->getElementById($id);
  119.  
  120.     if(!$element) {
  121.         throw new Exception("An element with id $id was not found");
  122.     }
  123.  
  124.     if($pretty) {
  125.         $doc->formatOutput = true;
  126.     }
  127.  
  128.     $srcElement = $element->getAttribute('src');
  129.  
  130.     if(isset($srcElement)) {
  131.         $srcElement = explode('?', $srcElement);
  132.         return $srcElement[0];
  133.     }
  134.  
  135.     return false;
  136. }
Add Comment
Please, Sign In to add comment