title: Copy images from url to server, delete all images after
body: <p>I have an affiliate link with a product image</p>
  
  <pre><code>http://www.myaffiliate.com/products/product.jpg
  </code></pre>
  
  <p>I want to copy the imagine to my website, in a folder called lets say products. It's hosted on shared sherver, not my pc.  I think the php copy() does not work, well tried it and I think my hosts doen't support the method.  How can I do it, curl maybe?  Then, I need a php command to delete all the images from the products folder - I'll do this with a cronjob.</p>
  
  <pre><code>&lt;?php
  $table = 'cron';
  
  $feed = 'myfeed';
  
  $xml = simplexml_load_file($feed);
  
  mysql_query("TRUNCATE TABLE ".$table."", $dbh1);
  mysql_query("TRUNCATE TABLE ".$table."", $dbh2);
  
  
  function getimg($url) {         
          $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';              
          $headers[] = 'Connection: Keep-Alive';         
          $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';         
          $user_agent = 'php';         
          $process = curl_init($url);         
          curl_setopt($process, CURLOPT_HTTPHEADER, $headers);         
          curl_setopt($process, CURLOPT_HEADER, 0);         
          curl_setopt($process, CURLOPT_USERAGENT, $useragent);         
          curl_setopt($process, CURLOPT_TIMEOUT, 30);         
          curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);         
          curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);         
          $return = curl_exec($process);         
          curl_close($process);         
          return $return;     
          } 
  
  foreach( $xml-&gt;performerinfo as $performerinfo )
  {
      $performerid = $performerinfo-&gt;performerid;
      $category = $performerinfo-&gt;category;
      $subcategory = $performerinfo-&gt;subcategory;
      $bio = $performerinfo-&gt;bio;
      $turnons = $performerinfo-&gt;turnons;
      $willing = $performerinfo-&gt;willingness;
      $willingness = str_replace(',', ', ', $willing); 
      $age = $performerinfo-&gt;age;
      $build = $performerinfo-&gt;build;
      $height = $performerinfo-&gt;height;
      $weight = $performerinfo-&gt;weight;
      $breastsize = $performerinfo-&gt;breastsize;
      $haircolor = $performerinfo-&gt;haircolor;
      $hairlength = $performerinfo-&gt;hairlength;
      $eyecolor = $performerinfo-&gt;eyecolor;
      $ethnicity = $performerinfo-&gt;ethnicity;
      $sexpref = $performerinfo-&gt;sexpref;
      $pic0 = $performerinfo-&gt;picture[0];
      $pic1 = $performerinfo-&gt;picture[1];
      $pic2 = $performerinfo-&gt;picture[2];
      $pic3 = $performerinfo-&gt;picture[3];
      $pic4 = $performerinfo-&gt;picture[4];   
      $test = $performerinfo-&gt;picture[0];  
  
      $imgurl = 'http://www.foodtest.ru/images/big_img/sausage_3.jpg'; 
      $imagename= basename($imgurl);
      if(file_exists('./tmp/'.$imagename)){continue;} 
      $image = getimg($imgurl); 
      file_put_contents('tmp/'.$imagename,$image); 
  
  }  
  
     //baracuda reloaded
     mysql_query("TRUNCATE TABLE reloaded", $dbh1);
     mysql_query("TRUNCATE TABLE reloaded", $dbh2);
  
     mysql_query("INSERT INTO reloaded SELECT * FROM ".$table."", $dbh1); 
     mysql_query("INSERT INTO reloaded SELECT * FROM ".$table."", $dbh2); 
  ?&gt;
  </code></pre>