我遇到了csv文件的问题。我想询问是否有方法如何从A列中的csv行(url)下载,然后将它们保存在B列的目录中,如果需要则覆盖图像。 例如:
http://data.militarysklad.cz/attach/eobchod/foto/125432dtc.jpg**;**/images/catalog/jjjj.jpg
答案 0 :(得分:0)
有两种方法:
1.
allow_url_fopen set to true
<?php
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$url = $data[0];
$path = $data[1];
file_put_contents($path, file_get_contents($url));
}
fclose($handle);
}
?>
2. cURL
<?php
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$url = $data[0];
$path = $data[1];
$ch = curl_init($url);
$fp = fopen($path, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
fclose($handle);
}
?>