我会假设 CSV 文件如下所示:
“文件名”;“网址”
“文件 1”;“f1 的 URL”
“文件 2”;“f2 的 URL”
“文件 3”;“f3 的 URL”
“文件 4”;“f4 的 URL”
“文件 5”;“f5 的 URL”
“文件 6”;“f6 的 URL”
所以列分隔符是;
和字符串分隔符"
所以代码会是这样的:
<?php
$fileContent = $file("path/to/csv/file"); // read the file to an array
array_shift( $fileContent ); // remove the first line, the header, from the array
foreach( $fileContent as $line ) {
$columns = str_getcsv( $line, ";", '"' );
$url = $columns[1]; // as suposed previously the url is in the second column
$filename = $columns[0];
downloadFile( $url, $filename );
}
function downloadFile( $url, $filename ) {
$newfname = "/path/to/download/folder/" . $filename ;
$file = fopen ($url, "rb");
if ($file) {
$newf = fopen ($newfname, "wb");
if ($newf)
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
}
}
if ($file) {
fclose($file);
}
if ($newf) {
fclose($newf);
}
}