1.通過 file_get_contents
$data = array( [1,'https://apply.jtw.beijing.gov.cn/apply/myimages/logo_xkc.png'], [2,'https://apply.jtw.beijing.gov.cn/apply/myimages/logo_xkc.png'] ); foreach($data as $key => $val){ $save_to='./file/' . $val[0]; // 把抓取的代碼寫入該文件 $save_to = iconv("UTF-8", "GBK", $save_to); if (!file_exists($save_to)){ mkdir ($save_to,0777,true);//創建目錄 } $file_url = $val[1]; $content = file_get_contents($file_url); file_put_contents($save_to . "/" . $val[0] . ".jpg", $content); }
2.通過 fopen
// 變量說明:
// $url 是遠程圖片的完整URL地址,不能為空。
// $filename 是可選變量: 如果為空,本地文件名將基於時間和日期// 自動生成.
*/
function GrabImage($url,$filename='') {
// echo $url;die;
if($url==''):return false;endif;
// if($filename=='') {
// $ext=strrchr($url,'.');
// if($ext!='.gif' && $ext!='.jpg'):return false;endif;$filename=date('dMYHis').$ext;
// }
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2=@fopen($filename, 'a');
@fwrite($fp2,$img);
@fclose($fp2);
}