How to download multiple files using php from csv as the references? -
kind of new php starting head around little.
what want do...
i have csv contains references files can download.
i found page: how download xml file through in php?
this enabled me download xml file if write url , directory want save no problems.
how modify php xml files in csv?
i assume like: foreach , variable functions etc have no idea how.
also in csv contains file name not full url, first part of url stay same. , same goes download directory. want files downloaded same directory pick , file name same 1 im downloading.
also how change php if example want download images, or other file type? assume easy?
thanks john
i suppose csv file following:
"filename";"url"
"file 1"; "url f1"
"file 2"; "url f2"
"file 3"; "url f3"
"file 4"; "url f4"
"file 5"; "url f5"
"file 6"; "url f6"
so column separator ;
, string separator "
so code like:
<?php $filecontent = $file("path/to/csv/file"); // read file array array_shift( $filecontent ); // remove first line, header, array foreach( $filecontent $line ) { $columns = str_getcsv( $line, ";", '"' ); $url = $columns[1]; // suposed url in 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); } }
Comments
Post a Comment