PHP -CSV从网址上读取和上传图片

时间:2008-11-24 18:11:42

标签: php import

任何人都可以帮我找到PHP中的代码片段,以读取CSV文件中的内容,然后插入到某些表格中,以及从www中获取一些数据(从CSV文件中获取图像网址)

1 个答案:

答案 0 :(得分:1)

代码看起来像这样。请注意,此代码尚未经过测试,也未加强安全性。

$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($handle);

//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

此代码假设您的csv看起来像这样。

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