2008-11-24 38 views
0

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

回答

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

此代码假定您的CSV看起来是这样的。

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