首頁 php教程 php手册 php实现远程文件下载代码

php实现远程文件下载代码

May 25, 2016 pm 04:57 PM
fopen

这里为各位提供一款远程文件下载代码,我们可以把远程的文件用php下载到本地指定的目录,希望文章对你有帮助。
 代码如下 复制代码

class download
{
    var http://pic4.phprm.com/2010/07/22/http://pic4.phprm.com/2010/07/22/$url.jpg.jpg;//远程文件地址

    var $file_name = "hdwiki.zip";//下载来的文件名称

    var $save_path = "./www.phprm.com";//下载到本地的文件路径

    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 ." 已经存在! 重新下载";
                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;
    }
}



教程网址:

欢迎收藏∩_∩但请保留本文链接。

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

如何解決PHP Warning: fopen(): failed to open stream: No such file or directory 如何解決PHP Warning: fopen(): failed to open stream: No such file or directory Aug 19, 2023 am 10:44 AM

如何解決PHP Warning: fopen(): failed to open stream: No such file or directory

如何解決PHP Warning: fopen(): SSL operation failed in file.php on line X 如何解決PHP Warning: fopen(): SSL operation failed in file.php on line X Aug 25, 2023 am 09:22 AM

如何解決PHP Warning: fopen(): SSL operation failed in file.php on line X

如何解決PHP Warning: fopen(): failed to open stream: Permission denied 如何解決PHP Warning: fopen(): failed to open stream: Permission denied Aug 20, 2023 pm 01:45 PM

如何解決PHP Warning: fopen(): failed to open stream: Permission denied

Matlab中fopen函數用法 Matlab中fopen函數用法 Nov 28, 2023 am 11:03 AM

Matlab中fopen函數用法

php如何使用fopen、fwrite和fclose進行檔案操作? php如何使用fopen、fwrite和fclose進行檔案操作? Jun 01, 2023 am 08:46 AM

php如何使用fopen、fwrite和fclose進行檔案操作?

在C語言中,使用fopen()函數以寫入模式開啟現有文件 在C語言中,使用fopen()函數以寫入模式開啟現有文件 Aug 27, 2023 pm 10:33 PM

在C語言中,使用fopen()函數以寫入模式開啟現有文件

如何解決PHP警告:fopen():無法開啟流:第X行的file.php中沒有這樣的檔案或目錄 如何解決PHP警告:fopen():無法開啟流:第X行的file.php中沒有這樣的檔案或目錄 Aug 26, 2023 pm 12:46 PM

如何解決PHP警告:fopen():無法開啟流:第X行的file.php中沒有這樣的檔案或目錄

php中fopen函數的用法是什麼 php中fopen函數的用法是什麼 Sep 18, 2021 pm 03:43 PM

php中fopen函數的用法是什麼

See all articles