谁能帮我在PHP中找出一个代码片段来读取CSV文件中的内容,然后插入到一些表中,以及从www中获取一些数据(从CSV文件中的图像url中获取图像)
发布于 2008-11-24 18:54:56
代码应该是这样的。注意:这段代码没有经过测试,也没有加强安全性。
$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);
This code assumes your csv looks like this.
1,http://www.foobar.com/images/something.jpghttps://stackoverflow.com/questions/314934
复制相似问题