PHP CURL实现远程下载文件到本地
PHP CURL实现远程下载文件到本地,该函数返回结果包括下载耗用的时间,保存的文件名,以及使用的下载方式。写的比较粗糙,如果大家有好的建议及改进方案,欢迎留言给我哦!
具体代码如下:
<?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; }
您可能感兴趣的文章
- php获取远程图片并下载保存到本地
- PHP 利用 Curl Functions 实现多线程抓取网页和下载文件
- php判断远程文件是否存在的办法
- php获取远程文件的大小
- php读取目录并列表显示目录中的文件的函数
- php清空(删除)指定目录下的文件,不删除目录文件夹的方法
- php获取目录所有文件并将结果保存到数组的程序
- 利用SecureCRT上传、下载文件(使用sz与rz命令)
Statement
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
How to fix KB5055612 fails to install in Windows 10?
1 months agoByDDD
Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Roblox: Grow A Garden - Complete Mutation Guide
3 weeks agoByDDD
Nordhold: Fusion System, Explained
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
