Home > Backend Development > PHP Tutorial > PHP CURL implements remote downloading of files to local_PHP tutorial

PHP CURL implements remote downloading of files to local_PHP tutorial

WBOY
Release: 2016-07-13 09:55:29
Original
1561 people have browsed it

PHP CURL implements remote downloading of files to the local place

PHP CURL implements remote downloading of files to the local area. The result returned by this function includes the time taken to download, the saved file name, and the usage download method. The writing is relatively rough. If you have good suggestions and improvement plans, please leave a message to me!

The specific code is as follows:

<?php

//$result=httpcopy('http://www.phpernote.com/image/logo.gif');

echo '<pre class="brush:php;toolbar:false">';print_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'=>'','way'=>'','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['way']='curl';
			$result['size']=sprintf('%.3f',strlen($temp)/1024);
        }
    }else{
        $opts=array(
            'http'=>array(
            'method'=>'GET',
            'header'=>'',
            'timeout'=>$timeout
			)
        );
        $context=stream_context_create($opts);
        if(@copy($url,$file,$context)){
            $result['fileName']=$file;
			$result['way']='copy';
			$result['size']=sprintf('%.3f',strlen($context)/1024);
        }
    }
	$endTime=explode(' ',microtime());
	$endTime=(float)$endTime[0]+(float)$endTime[1];
	$result['spendTime']=round($endTime-$startTime)*1000;//单位:毫秒
	return $result;
}
Copy after login

Articles you may be interested in

  • php obtains remote images and downloads them and saves them locally
  • PHP uses Curl Functions to implement multi-threaded web crawling and file downloading
  • How php determines whether a remote file exists
  • php gets the size of a remote file
  • php function to read a directory and list the files in the directory
  • php method to clear (delete) files in a specified directory without deleting the directory folder
  • php program to get all the files in the directory and save the results to an array
  • Use SecureCRT to upload and download files ( Use sz and rz commands)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/992743.htmlTechArticlePHP CURL realizes remote downloading of files to local PHP CURL realizes remote downloading of files to local. The result returned by this function includes downloading time The time used, the file name saved, and the download method used...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template