search
Homephp教程php手册php实现远程文件下载代码

这里为各位提供一款远程文件下载代码,我们可以把远程的文件用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;
    }
}



教程网址:

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

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
如何解决PHP Warning: fopen(): failed to open stream: No such file or directory如何解决PHP Warning: fopen(): failed to open stream: No such file or directoryAug 19, 2023 am 10:44 AM

如何解决PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory在使用PHP开发过程中,我们经常会遇到一些文件操作的问题,其中之一就是"PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory

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

如何解决PHPWarning:fopen():SSLoperationfailedinfile.phponlineX在PHP编程中,我们经常使用fopen函数来打开文件或者URL,并进行相关操作。然而,在使用fopen函数时,有时候会遇到类似于Warning:fopen():SSLoperationfailedinfile.p

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

如何解决PHPWarning:fopen():failedtoopenstream:Permissiondenied在开发PHP程序的过程中,我们常常会遇到一些报错信息,比如PHPWarning:fopen():failedtoopenstream:Permissiondenied。这个错误通常是由于文件或目录权限不正

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函数和fread函数,文件的写入可以使用fopen函数、fwrite函数和fclose函数。本文将介绍php如何使用fopen、fwrite和fclose进行文件操作。一、fopen函数fopen函数用于打开文件,它的语法如下:r

在C语言中,使用fopen()函数以写模式打开现有文件在C语言中,使用fopen()函数以写模式打开现有文件Aug 27, 2023 pm 10:33 PM

C中的fopen()方法用于打开指定的文件。我们举个例子来理解一下问题语法FILE*fopen(filename,mode)以下是使用fopen()打开文件的有效模式:‘r’、‘w’、‘a’、‘r+’、‘w+’、‘a+’。详细信息请访问C库函数-fopen()

如何解决PHP Warning: fopen(): failed to open stream: No such file or directory in file.php on line X如何解决PHP Warning: fopen(): failed to open stream: No such file or directory in file.php on line XAug 26, 2023 pm 12:46 PM

如何解决PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectoryinfile.phponlineX在开发和运行PHP程序时,我们有时会遇到PHPWarning:fopen():failedtoopenstream:Nosuchfileor

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

php中fopen函数的用法是“fopen(filename,mode,include_path,context)”。fopen()函数用来打开一个文件或者是url,如果失败则返回false并附带错误信息。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software