本文实例讲述了PHP实现的下载远程文件类定义与用法。分享给大家供大家参考,具体如下:
<?php
/**
* 下载远程文件类支持断点续传
*/
class HttpDownload {
private $m_url = "";
private $m_urlpath = "";
private $m_scheme = "http";
private $m_host = "";
private $m_port = "80";
private $m_user = "";
private $m_pass = "";
private $m_path = "/";
private $m_query = "";
private $m_fp = "";
private $m_error = "";
private $m_httphead = "" ;
private $m_html = "";
/**
* 初始化
*/
public function PrivateInit($url){
$urls = "";
$urls = @parse_url($url);
$this->m_url = $url;
if(is_array($urls)) {
$this->m_host = $urls["host"];
if(!empty($urls["scheme"])) $this->m_scheme = $urls["scheme"];
if(!empty($urls["user"])) $this->m_user = $urls["user"];
if(!empty($urls["pass"])) $this->m_pass = $urls["pass"];
if(!empty($urls["port"])) $this->m_port = $urls["port"];
if(!empty($urls["path"])) $this->m_path = $urls["path"];
$this->m_urlpath = $this->m_path;
if(!empty($urls["query"])) {
$this->m_query = $urls["query"];
$this->m_urlpath .= "?".$this->m_query;
}
}
}
/**
* 打开指定网址
*/
function OpenUrl($url) {
#重设各参数
$this->m_url = "";
$this->m_urlpath = "";
$this->m_scheme = "http";
$this->m_host = "";
$this->m_port = "80";
$this->m_user = "";
$this->m_pass = "";
$this->m_path = "/";
$this->m_query = "";
$this->m_error = "";
$this->m_httphead = "" ;
$this->m_html = "";
$this->Close();
#初始化系统
$this->PrivateInit($url);
$this->PrivateStartSession();
}
/**
* 获得某操作错误的原因
*/
public function printError() {
echo "错误信息:".$this->m_error;
echo "具体返回头:<br>";
foreach($this->m_httphead as $k=>$v) {
echo "$k => $v <br>\r\n";
}
}
/**
* 判别用Get方法发送的头的应答结果是否正确
*/
public function IsGetOK() {
if( ereg("^2",$this->GetHead("http-state")) ) {
return true;
} else {
$this->m_error .= $this->GetHead("http-state")." - ".$this->GetHead("http-describe")."<br>";
return false;
}
}
/**
* 看看返回的网页是否是text类型
*/
public function IsText() {
if (ereg("^2",$this->GetHead("http-state")) && eregi("^text",$this->GetHead("content-type"))) {
return true;
} else {
$this->m_error .= "内容为非文本类型<br>";
return false;
}
}
/**
* 判断返回的网页是否是特定的类型
*/
public function IsContentType($ctype) {
if (ereg("^2",$this->GetHead("http-state")) && $this->GetHead("content-type") == strtolower($ctype)) {
return true;
} else {
$this->m_error .= "类型不对 ".$this->GetHead("content-type")."<br>";
return false;
}
}
/**
* 用 HTTP 协议下载文件
*/
public function SaveToBin($savefilename) {
if (!$this->IsGetOK()) return false;
if (@feof($this->m_fp)) {
$this->m_error = "连接已经关闭!";
return false;
}
$fp = fopen($savefilename,"w") or die("写入文件 $savefilename 失败!");
while (!feof($this->m_fp)) {
@fwrite($fp,fgets($this->m_fp,256));
}
@fclose($this->m_fp);
return true;
}
/**
* 保存网页内容为 Text 文件
*/
public function SaveToText($savefilename) {
if ($this->IsText()) {
$this->SaveBinFile($savefilename);
} else {
return "";
}
}
/**
* 用 HTTP 协议获得一个网页的内容
*/
public function GetHtml() {
if (!$this->IsText()) return "";
if ($this->m_html!="") return $this->m_html;
if (!$this->m_fp||@feof($this->m_fp)) return "";
while(!feof($this->m_fp)) {
$this->m_html .= fgets($this->m_fp,256);
}
@fclose($this->m_fp);
return $this->m_html;
}
/**
* 开始 HTTP 会话
*/
public function PrivateStartSession() {
if (!$this->PrivateOpenHost()) {
$this->m_error .= "打开远程主机出错!";
return false;
}
if ($this->GetHead("http-edition")=="HTTP/1.1") {
$httpv = "HTTP/1.1";
} else {
$httpv = "HTTP/1.0";
}
fputs($this->m_fp,"GET ".$this->m_urlpath." $httpv\r\n");
fputs($this->m_fp,"Host: ".$this->m_host."\r\n");
fputs($this->m_fp,"Accept: */*\r\n");
fputs($this->m_fp,"User-Agent: Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.2)\r\n");
#HTTP1.1协议必须指定文档结束后关闭链接,否则读取文档时无法使用feof判断结束
if ($httpv=="HTTP/1.1") {
fputs($this->m_fp,"Connection: Close\r\n\r\n");
} else {
fputs($this->m_fp,"\r\n");
}
$httpstas = fgets($this->m_fp,256);
$httpstas = split(" ",$httpstas);
$this->m_httphead["http-edition"] = trim($httpstas[0]);
$this->m_httphead["http-state"] = trim($httpstas[1]);
$this->m_httphead["http-describe"] = "";
for ($i=2;$i<count($httpstas);$i++) {
$this->m_httphead["http-describe"] .= " ".trim($httpstas[$i]);
}
while (!feof($this->m_fp)) {
$line = str_replace("\"","",trim(fgets($this->m_fp,256)));
if($line == "") break;
if (ereg(":",$line)) {
$lines = split(":",$line);
$this->m_httphead[strtolower(trim($lines[0]))] = trim($lines[1]);
}
}
}
/**
* 获得一个Http头的值
*/
public function GetHead($headname) {
$headname = strtolower($headname);
if (isset($this->m_httphead[$headname])) {
return $this->m_httphead[$headname];
} else {
return "";
}
}
/**
* 打开连接
*/
public function PrivateOpenHost() {
if ($this->m_host=="") return false;
$this->m_fp = @fsockopen($this->m_host, $this->m_port, &$errno, &$errstr,10);
if (!$this->m_fp){
$this->m_error = $errstr;
return false;
} else {
return true;
}
}
/**
* 关闭连接
*/
public function Close(){
@fclose($this->m_fp);
}
}
#两种使用方法,分别如下:
#打开网页
$httpdown = new HttpDownload();
$httpdown->OpenUrl("http://www.google.com.hk");
echo $httpdown->GetHtml();
$httpdown->Close();
#下载文件
$file = new HttpDownload(); # 实例化类
$file->OpenUrl("http://www.ti.com.cn/cn/lit/an/rust020/rust020.pdf"); # 远程文件地址
$file->SaveToBin("rust020.pdf"); # 保存路径及文件名
$file->Close(); # 释放资源
?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php文件操作总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
# PHP
# 下载
# 远程文件
# 类
# php下载远程大文件(获取远程文件大小)的实例
# 浅谈php fopen下载远程文件的函数
# 从性能方面考虑PHP下载远程文件的3种方法
# PHP下载远程文件到本地存储的方法
# php带密码功能并下载远程文件保存本地指定目录 修改加强版
# php下载远程文件类(支持断点续传)
# PHP实现的文件操作类及文件下载功能示例
# php实现的支持断点续传的文件下载类
# PHP文件下载类
# 解决PHP超大文件下载
# 断点续传下载的方法详解
# 程序设计
# 操作技巧
# 文档
# 相关内容
# 两种
# 感兴趣
# 给大家
# 更多关于
# 错误信息
# 所述
# 面向对象
# 是否正确
# 断点续传
# 打开网页
# 结束后
# 编程技巧
# 讲述了
# query
# path
# port
相关文章:
测试制作网站有哪些,测试性取向的权威测试或者网站?
如何用西部建站助手快速创建专业网站?
C#怎么创建控制台应用 C# Console App项目创建方法
正规网站制作公司有哪些,目前国内哪家网页网站制作设计公司比较专业靠谱?口碑好?
建站之星IIS配置教程:代码生成技巧与站点搭建指南
如何通过.red域名打造高辨识度品牌网站?
如何使用Golang table-driven基准测试_多组数据测量函数效率
建站主机如何选?高性价比方案全解析
建站DNS解析失败?如何正确配置域名服务器?
制作网站的软件下载免费,今日头条开宝箱老是需要下载怎么回事?
头像制作网站在线观看,除了站酷,还有哪些比较好的设计网站?
微信小程序 五星评分(包括半颗星评分)实例代码
详解ASP.NET 生成二维码实例(采用ThoughtWorks.QRCode和QrCode.Net两种方式)
如何在景安服务器上快速搭建个人网站?
如何用y主机助手快速搭建网站?
济南网站建设制作公司,室内设计网站一般都有哪些功能?
外贸公司网站制作,外贸网站建设一般有哪些步骤?
网站网页制作电话怎么打,怎样安装和使用钉钉软件免费打电话?
c# Task.Yield 的作用是什么 它和Task.Delay(1)有区别吗
建站之星导航如何优化提升用户体验?
如何自定义建站之星模板颜色并下载新样式?
小米网站链接制作教程,请问miui新增网页链接调用服务有什么用啊?
网页设计与网站制作内容,怎样注册网站?
如何快速上传建站程序避免常见错误?
制作旅游网站html,怎样注册旅游网站?
高端建站三要素:定制模板、企业官网与响应式设计优化
上海网站制作网站建设公司,建筑电工证网上查询系统入口?
建站之星如何开启自定义404页面避免用户流失?
JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)
建站之星如何实现网站加密操作?
,想在网上投简历,哪几个网站比较好?
企业网站制作公司网页,推荐几家专业的天津网站制作公司?
网站制作需要会哪些技术,建立一个网站要花费多少?
学校建站服务器如何选型才能满足性能需求?
如何用腾讯建站主机快速创建免费网站?
网站制作的软件有哪些,制作微信公众号除了秀米还有哪些比较好用的平台?
免费制作海报的网站,哪位做平面的朋友告诉我用什么软件做海报比较好?ps还是cd还是ai这几个软件我都会些我是做网页的?
如何用IIS7快速搭建并优化网站站点?
如何用手机制作网站和网页,手机移动端的网站能制作成中英双语的吗?
公众号网站制作网页,微信公众号怎么制作?
如何用PHP工具快速搭建高效网站?
专业企业网站设计制作公司,如何理解商贸企业的统一配送和分销网络建设?
如何在VPS电脑上快速搭建网站?
Swift开发中switch语句值绑定模式
网站制作多少钱一个,建一个论坛网站大约需要多少钱?
视频网站制作教程,怎么样制作优酷网的小视频?
建站之星如何实现五合一智能建站与营销推广?
南京网站制作费用,南京远驱官方网站?
子杰智能建站系统|零代码开发与AI生成SEO优化指南
开心动漫网站制作软件下载,十分开心动画为何停播?
*请认真填写需求信息,我们会在24小时内与您取得联系。