PHP远程下载类
时间:2009-03-10
来源:互联网
PHP远程下载类
<?php
class DownLoad
{
var $url;//远程文件地址
var $file_name = "hdwiki.zip";//下载来的文件名称
var $save_path = "./updatefile";//下载到本地的文件路径
var $localfile;//下载到本地文件的路径和名称
var $warning;//警告信息
var $redown=0;//是否重新下载
/*初始化*/
function setUrl($url)
{
if(!empty($url))$this->url = $url;
}
function setFileName($file_name)
{
if(!empty($file_name))$this->file_name = $file_name;
}
function setSavePath($save_path)
{
if(!empty($save_path))$this->save_path = $save_path;
}
function setRedown($redown)
{
if(!empty($redown))$this->redown = $redown;
}
function DownLoad($url, $redown = 0, $save_path = 0, $file_name = 0)
{
$this->setUrl($url);
$this->setFileName($file_name);
$this->setSavePath($save_path);
$this->setRedown($redown);
if(!file_exists($this->save_path))
{
$dir = explode("/",$this->save_path);
foreach($dir as $p)
mkdir($p);
}
}
/* 检查URL合法性函数 */
function checkUrl(){
return preg_match("/^(http|ftp)(:\/\/)([a-zA-Z0-9-_]+[\.\/]+[\w\-_\/]+.*)+$/i", $this->url);
}
//下载文件到本地
function downLoadFile()
{
//检测变量
$this->localfile = $this->save_path."/".$this->file_name;
if($this->url == "" || $this->localfile == ""){
$this->warning = "Error: 变量设置错误.";
return $this->warning;
}
if (!$this->checkUrl()){
$this->warning = "Error: URL ". $this->url ." 不合法.";
return $this->warning;
}
if (file_exists($this->localfile)){
if($this->redown)
{
unlink($this->localfile);
}
else
{
$this->warning = "Warning: 升级文件 ". $this->localfile ." 已经存在! <a href='?action=download&redown=1' target='_self'>重新下载</a>";
return $this->warning;
//exit("Error: 本地文件 ". $this->localfile ." 已经存在,请删除或改名后重新运行本程序.");
}
}
//打开远程文件
$fp = fopen($this->url, "rb");
if (!$fp){
$this->warning = "Error: 打开远程文件 ". $this->url ." 失败.";
return $this->warning;
}
//打开本地文件
$sp = fopen($this->localfile, "wb");
if (!$sp){
$this->warning = "Error: 打开本地文件 ". $this->localfile ." 失败.";
return $this->warning;
}
//下载远程文件
//echo "正在下载远程文件,请等待";
while (!feof($fp)){
$tmpfile .= fread($fp, 1024);
//echo strlen($tmpfile);
}
//保存文件到本地
fwrite($sp, $tmpfile);
fclose($fp);
fclose($sp);
if($this->redown)
$this->warning = "Success: 重新下载文件 ". $this->file_name ." 成功";
else
$this->warning = "Success: 下载文件 ". $this->file_name ." 成功";
return $this->warning;
}
}
?>
<?php
class DownLoad
{
var $url;//远程文件地址
var $file_name = "hdwiki.zip";//下载来的文件名称
var $save_path = "./updatefile";//下载到本地的文件路径
var $localfile;//下载到本地文件的路径和名称
var $warning;//警告信息
var $redown=0;//是否重新下载
/*初始化*/
function setUrl($url)
{
if(!empty($url))$this->url = $url;
}
function setFileName($file_name)
{
if(!empty($file_name))$this->file_name = $file_name;
}
function setSavePath($save_path)
{
if(!empty($save_path))$this->save_path = $save_path;
}
function setRedown($redown)
{
if(!empty($redown))$this->redown = $redown;
}
function DownLoad($url, $redown = 0, $save_path = 0, $file_name = 0)
{
$this->setUrl($url);
$this->setFileName($file_name);
$this->setSavePath($save_path);
$this->setRedown($redown);
if(!file_exists($this->save_path))
{
$dir = explode("/",$this->save_path);
foreach($dir as $p)
mkdir($p);
}
}
/* 检查URL合法性函数 */
function checkUrl(){
return preg_match("/^(http|ftp)(:\/\/)([a-zA-Z0-9-_]+[\.\/]+[\w\-_\/]+.*)+$/i", $this->url);
}
//下载文件到本地
function downLoadFile()
{
//检测变量
$this->localfile = $this->save_path."/".$this->file_name;
if($this->url == "" || $this->localfile == ""){
$this->warning = "Error: 变量设置错误.";
return $this->warning;
}
if (!$this->checkUrl()){
$this->warning = "Error: URL ". $this->url ." 不合法.";
return $this->warning;
}
if (file_exists($this->localfile)){
if($this->redown)
{
unlink($this->localfile);
}
else
{
$this->warning = "Warning: 升级文件 ". $this->localfile ." 已经存在! <a href='?action=download&redown=1' target='_self'>重新下载</a>";
return $this->warning;
//exit("Error: 本地文件 ". $this->localfile ." 已经存在,请删除或改名后重新运行本程序.");
}
}
//打开远程文件
$fp = fopen($this->url, "rb");
if (!$fp){
$this->warning = "Error: 打开远程文件 ". $this->url ." 失败.";
return $this->warning;
}
//打开本地文件
$sp = fopen($this->localfile, "wb");
if (!$sp){
$this->warning = "Error: 打开本地文件 ". $this->localfile ." 失败.";
return $this->warning;
}
//下载远程文件
//echo "正在下载远程文件,请等待";
while (!feof($fp)){
$tmpfile .= fread($fp, 1024);
//echo strlen($tmpfile);
}
//保存文件到本地
fwrite($sp, $tmpfile);
fclose($fp);
fclose($sp);
if($this->redown)
$this->warning = "Success: 重新下载文件 ". $this->file_name ." 成功";
else
$this->warning = "Success: 下载文件 ". $this->file_name ." 成功";
return $this->warning;
}
}
?>
作者: php网站 发布时间: 2009-03-10
顶一下
作者: ccdsw 发布时间: 2009-09-01
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28