PHP -CSV Execute and load images from url

Can anyone help me find some code snippet in PHP to read the content in a CSV file and then insert into some tables and also get some data from www (fetch image fom url in CSV file)

0


source to share


1 answer


The code will look something like this. Please note that this code has not been tested or hardened for security.




      



$handle = fopen("fileUploads.csv", "r");





while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $insertSQL = "INSERT INTO myTable values ('$data[0]', '$data[1]')";





  //run the sql

  //download the image
  $saveHandle = fopen("image".$data[0] .".jpg", "w+");

  $getImageHandle = fopen($data[1], "r"); // open the image

  $contents = stream_get_contents($getImageHandle); //download it

  fclose($getImageHandle); //close the handle

  fwrite($saveHandle, $contents); //write the contents to disk

  fclose($saveHandle); //close the handle

      







}





fclose ($ );





>



This code assumes your csv looks like this.

1, http://www.foobar.com/images/something.jpg

+1


source







All Articles