title: Save image from url with curl and file_put_contents PHP
body: <p>i want to save a picture from a remote server to my site. 
  I create the text in the TinyMCE editor and insert the image from the remote server there. Next, I need to save this picture to my server.
  To do this, i get a link to the picture from the text:    </p>
  
  <pre class="lang-php prettyprint-override"><code>    preg_match('/&lt;img(.*)src(.*)=(.*)"(.*)"/U', $text, $result);
      $url =  array_pop($result);
  </code></pre>
  
  <p>Next through curl and <code>file_put_contents</code> i get the file and copy to my server.</p>
  
  <pre class="lang-php prettyprint-override"><code>    $headers = array();
      $headers[] = 'Content-Type: image/jpeg';
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL,  $url ) ;
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; 
      Windows NT 5.0)");
      $image = curl_exec($ch);
      curl_close($ch);
  
      file_put_contents('myfolder/image.jpg', $url);
  </code></pre>
  
  <p>As a result, not a picture is created, but a text file 'myfolder/image.jpg' with a size of 16 kb with text - Bad URL timestamp.</p>
  
  <pre class="lang-none prettyprint-override"><code>curl_getinfo returns [content_type] =&gt; text/plain [http_code] =&gt; 403
  </code></pre>
  
  <p>But, in case i am assigning <code>$url</code> manually in <code>CURLOPT_URL</code>, for example </p>
  
  <pre class="lang-php prettyprint-override"><code>$url = 'https://scontent.ftbs4-1.fna.fbcdn.net/v/t1.0-9/39900479_1856467244440953_5986986678919626752_n.jpg?_nc_cat=0&amp;oh=6262ebe636e7328f0471af2820fd4050&amp;oe=5C03BEC7'
  </code></pre>
  
  <p>then the file is successfully copied.</p>
  
  <pre class="lang-none prettyprint-override"><code>curl_getinfo returns [content_type] =&gt; image/jpeg [http_code] =&gt; 200 
  </code></pre>
  
  <p>Where did I do wrong?</p>
  
  <p>This <code>$_POST</code>:</p>
  
  <pre class="lang-none prettyprint-override"><code>Array ( 
    [id] =&gt; 143
    [title] =&gt; Topic
    [description] =&gt; description
    [text] =&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; &lt;p&gt;Hello&lt;/p&gt; &lt;p&gt;&lt;img src="https://scontent.ftbs4-1.fna.fbcdn.net/v/t1.0-9/39900479_1856467244440953_5986986678919626752_n.jpg?_nc_cat=0&amp;amp;oh=6262ebe636e7328f0471af2820fd4050&amp;amp;oe=5C03BEC7" alt="" width="776" height="776" /&gt;&lt;/p&gt; &lt;/body&gt; &lt;/html&gt;
  )
  </code></pre>
  
  <p>full php code </p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php 
  //print_r($_POST);
  
  preg_match_all('/&lt;img[^&gt;]+&gt;/i',$_POST['text'] , $result); 
  
  foreach($result  as $img_tag){
  foreach( $img_tag as $tag){   
  preg_match('/&lt;img(.*)src(.*)=(.*)"(.*)"/U', $tag, $regexResult);
  $img_link = array_pop($regexResult);
  $file_name = basename($img_link);
  
  //$img_link = 'https://scontent.ftbs4-1.fna.fbcdn.net/v/t1.0-9/39900479_1856467244440953_5986986678919626752_n.jpg?_nc_cat=0&amp;oh=6262ebe636e7328f0471af2820fd4050&amp;oe=5C03BEC7';
  
  $headers = array();
  $headers[] = 'Content-Type: image/jpeg';
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,  $img_link ) ;
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");                                                                   
  $html = curl_exec($ch);
  curl_close($ch);
  
  $targetPath = '/folder/'.$_POST['id'].'/';
  
  file_put_contents($targetPath.$file_name, $html);
  }}  
  ?&gt;
  </code></pre>