日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

PHP远程下载类

發布時間:2025/5/22 php 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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;
????}
}
?>?

轉載于:https://blog.51cto.com/3961409/809474

總結

以上是生活随笔為你收集整理的PHP远程下载类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。