title: php rename image saved from url based on csv values
body: <p>currently: my code saves images from URLs given in a csv file.</p>
  
  <p><strong>goal</strong>: rename saved images according to values in the same csv file, and formatted by: csv positions 3,4,5,6,7,8
  <br>(e.g. 100A1_https://www.instagram.com/p/BBzUXUFLrGH/_48.8486557_2.3481125).</p>
  
  <p>i'm trying to merge <a href="https://stackoverflow.com/questions/5114310/php-rename-files-from-csv">this stackoverflow</a> example into my code. but unable to, as i am hopelessly lost.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>&lt;?php
  
  $destination = 'images/';
  $dom = new DOMDocument;
  
  $dir = "dir to photos";
  
  $row = 0;
  
  if (($handle = fopen("data/testcsvfile.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 100, ",")) !== FALSE /*&amp;&amp; $row &lt; 1*/) {
      $row++;
  
      // number of fields in each row
      $num = count($data);
  
      // get url from position in csv
      $url = $data[6];
      echo $row . " " . $url. PHP_EOL;
  
      if( strlen($url) &gt; 0 ){
          // if we have a url, get the contents
          $page = file_get_contents($url);
          //echo $page;
  
          $dom-&gt;loadHTML($page);
  
          // find url using meta tag
          $my_tags = $dom-&gt;getElementsByTagName('meta');
  
          // find which one is the image and grab and save
          foreach ($my_tags as $tag) {
              if($tag-&gt;getAttribute("property") == "og:image"){
                  $image_url = $tag-&gt;getAttribute('content');
                  echo  $image_url . PHP_EOL;
                  $the_image = file_get_contents($image_url);
  
                  // rename file based on csv
                  // $newname = "$dir"."$names[3] $names[4] $names[5]".";
                  rename($newname);
  
  
                  file_put_contents( $destination . "img_" . $row . ".jpg", $the_image);
  
              }
  
          }
  
      }
  
    }
    fclose($handle);
  }
  
  ?&gt;</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>// test csv file
  
  Mon,1,0,100,A,1,https://www.instagram.com/p/BBzUXUFLrGH/,48.8486557,2.3481125
  Mon,1,0,100,A,1,https://www.instagram.com/p/BAe0tGULrC1/,48.85272468,2.347259349
  Mon,1,0,100,A,1,https://www.instagram.com/p/_zik5YLrMf/,48.85356691,2.345645975</code></pre>
  </div>
  </div>
  </p>