<?php//$result =httpcopy (' http://www.phpernote.com/image/logo.gif '); Echo ' <pre> ';p rint_r ($result); function Httpcopy ($url, $file = ", $timeout =60) {$file =empty ($file)? PathInfo ($url, pathinfo_basename): $file; $dir =pathinfo ($file, pathinfo_dirname); !is_dir ($dir) && @mkdir ($dir, 0755,true); $url =str_replace (', ', '%20 ', $url); $result =array (' fileName ' = ' = ', ' The ' ' "' = ' = ', ' size ' =>0, ' spendtime ' =>0); $ Starttime=explode (', microtime ()); $startTime = (float) $startTime [0]+ (float) $startTime [1]; if (function_exists (' Curl_init ')) {$ch =curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_timeout, $timeout); curl_setopt ($ch, curlopt_returntransfer,true); $temp =curl_exec ($ch); if (@file_put_contents ($file, $temp) &&!curl_error ($ch)) {$result [' fileName ']= $file; $result [' On ' the ']= ' curl '; $result [' Size ']=sprintf ('%.3f ', strlen ($temp)/1024); }}else{$opts =array (' the ' http ' =>array (' method ')= ' GET ', ' header ' = ' ', ' timeout ' = $timeout)); $context =stream_context_create ($opts); if (@copy ($url, $file, $context)) {$result [' fileName ']= $file; $result [']= ' copy '; $result [' Size ']=sprintf ('%.3f ', strlen ($context)/1024); }} $endTime =explode (' ', Microtime ()), $endTime = (float) $endTime [0]+ (float) $endTime [1]; $result [' Spendtime ']=round ($ endtime-$startTime) *1000;//unit: milliseconds return $result;}
PHP curl enables remote download of files to local, the function returns results including the time of download, the name of the saved file, and the way to download it. Write more rough, if you have good suggestions and improve the program, welcome to leave a message to me Oh!
PHP curl enables remote download of files to local