title: Copy image from URL, change name and save to folder
body: <p>Im about to start on a PHP script to import csv database.</p>
  
  <p>The csv has a column with urls to product images.</p>
  
  <p>What I need to be able to do is get the image, check what type of file it is (jpg, png etc), change the name, save the file to a folder on the server and then insert the file name into the database.</p>
  
  <p>The inserting into the database bit I can do, its the renaming of the file im confused with.</p>
  
  <p>Is it possible to grab the info like you would when uploading a file, for example:</p>
  
  <p>Uploading a file using a file input in a html form</p>
  
  <pre><code>$_FILES['file']['name'];
  or
  $_FILES['file']['type'];
  </code></pre>
  
  <p>If downloading a file, could this be possible</p>
  
  <pre><code>$downloaded_image['name'];
  or
  $downloaded_image['type'];
  </code></pre>
  
  <p>or is that totally off the mark?</p>
  
  <p>I have never done this before, and most of the answers on stackoverflow don't quite answer my question so was hoping someone could point me in the right direction on how to do this.</p>
  
  <p>EDITED/UPDATED:</p>
  
  <p>Would something like this work to get the file attributes...</p>
  
  <pre><code>$image_id = '123456';
  $the_image = file_get_contents($downloaded_image);
  $image_name = $the_image['name'];
  $image_type = $the_image['type'];
  
  $new_name = $image_id . '.' . $image_type;
  
  $img_path = '/images/';
  $save_image = file_put_contents($img_path, $new_name);
  if($save_image) {
  echo 'image saved';
  } else {
  echo 'Not Saved';
  }
  </code></pre>
  
  <p>Hopefully im making some sense.</p>
  
  <p>UPDATE: here is the script as it is (still needs tidying up)</p>
  
  <pre><code>define('CSV_PATH','E:/xampp/htdocs/aff/csv-import/');
  // path where your CSV file is located
  
  $csv_file = CSV_PATH . "infotuts.csv"; // Name of your CSV file
  $csvfile = fopen($csv_file, 'r');
  $theData = fgets($csvfile);
  $i = 0;
  while (!feof($csvfile)) {
      $csv_data[] = fgets($csvfile, 1024);
      $csv_array = explode(",", $csv_data[$i]);
      $insert_csv = array();
      $insert_csv['test_id'] = $csv_array[0];
      // $insert_csv['test_aw_id'] = $csv_array[1];
      // $insert_csv['test_name'] = $csv_array[2];
      $image_id = $csv_array[1];
      $download_image = $csv_array[2];
  
      // Store the original filename
      $original_name = basename($download_image);
  
      // Original extension by string manipulation
      $original_extension = substr($original_name, strrpos($original_name, '.')); // ".jpg"
  
      // An array to match mime types from finfo_file() with extensions
      // Use of finfo_file() is recommended if you can't trust the input
      // filename's extension
      $types = array('image/jpeg' =&gt; '.jpg','image/png' =&gt; '.png','image/gif' =&gt; '.gif');
  
      // Get the file and save it
      $img = file_get_contents($download_image);
      $stored_name = 'images/' . $image_id . $original_extension;
      if ($img) {
        file_put_contents($stored_name);
  
        // Get the filesize if needed
        $size = filesize($stored_name);
  
        // If you don't care about validating the mime type, skip all of this...
        // Check the file information
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mimetype = finfo_file($finfo, $stored_name);
  
        // Lookup the type in your array to get the extension
        if (isset($types[$mimetype])) {
          // if the reported type doesn't match the original extension, rename the file
          if ($types[$mimetype] != $original_extension) {
            rename($stored_name, 'images/' . $image_id . $types[$mimetype]);
          }
        }
        else {
          // unknown type, handle accordingly...
        }
        finfo_close($finfo);
  
          $query = "INSERT INTO test(test_id, test_aw_id, test_name) VALUES ('', '$image_id', '$stored_name')";
          $n=mysqli_query($con, $query);
          $i++;
      }
      else {
        echo 'Could not get file';
      }
  
  }
  fclose($csvfile);
  </code></pre>