title: Save images from url with variable
body: <p><strong>After your help, many stackoverflow posts, the solution is at the bottom of this post as an UPDATE.</strong></p>
  
  <p>I am trying to save some images automatically using this code in a php file :</p>
  
  <pre><code>for ($num1=100;$num1&lt;999;$num1++)
  {
      for ($num2=100;$num2&lt;999;$num2++)
      {
          $postURL = "http://link_00000'.$num1.'_'.$num2.'.jpg";
          $ch = curl_init('http://link_00000'.$num1.'_'.$num2.'.jpg');
          $fp = fopen($postURL, '/path/Apolo/img/'.$num1.'_'.$num2.'.jpg', 'wb');
          curl_setopt($ch, CURLOPT_FILE, $fp);
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_exec($ch);
          curl_close($ch);
          fclose($fp);
      }
  }
  </code></pre>
  
  <blockquote>
    <p>First question :</p>
  </blockquote>
  
  <p><code>$num1</code> and <code>$num2</code> should start from 000 to 999 and not from 0 to 999. Putting more <code>$num</code> variables (<code>$num3</code>, <code>$num4</code> ...) would be a solution but I presume there is a better one for the digits. </p>
  
  <blockquote>
    <p>Second question :</p>
  </blockquote>
  
  <p>The images are not saved. I tried this one as well but it did not work:</p>
  
  <p><code>copy($postURL, '/path/img/'.$num1.'_'.$num2.'.jpg');</code></p>
  
  <blockquote>
    <p>Third question :</p>
  </blockquote>
  
  <p>How can I prevent an action like d-dos attack? If I load the links without a time delay, probably the website will be down. </p>
  
  <hr>
  
  <p>UPD:</p>
  
  <pre><code>for ($num1=000;$num1&lt;999;$num1++)
  {
      for ($num2=000;$num2&lt;999;$num2++)
      {
          $url = 'http://link.com/00000'.sprintf("%03d", $num1).'_'.sprintf("%03d", $num2).'.jpg';
          echo ''.$num1.'_'.$num2.'';
          echo "\n";
          if (@getimagesize($url)) \\ checks if url-image exists
          {   
              echo $url;
              $ch = curl_init($url);
              $fp = fopen('/path/Apolo/00000'.sprintf("%03d", $num1).'_'.sprintf("%03d", $num2).'.jpg', 'wb'); 
              curl_setopt($ch, CURLOPT_FILE, $fp); 
              curl_setopt($ch, CURLOPT_HEADER, 0); 
              curl_exec($ch); 
              curl_close($ch); 
              fclose($fp);
              sleep(1); //1 second delay to avoid d-dos
          }
      }
  }
  </code></pre>