How use CURLOPT_WRITEFUNCTION when download a file by CURL

后端 未结 4 1548
陌清茗
陌清茗 2021-01-20 04:10

My Class for download file direct from a link:

MyClass{

          function download($link){
                ......
                $ch = curl_init($link);
          


        
4条回答
  •  粉色の甜心
    2021-01-20 04:51

    Why do you use curl to download a file? Is there a special reason? You can simply use fopen and fread

    I have written a small class for it.

    source = $source;
            $this->dest   = $dest;
            $this->buffer = $buffer; 
            $this->overwrite = $overwrite;
        }
        public function download(){
            if($this->overwrite||!file_exists($this->dest)){
                if(!is_dir(dirname($this->dest))){mkdir(dirname($this->dest),0755,true);}
                if($this->source==""){
                    $resource = false;
                    Utils_Logging_Logger::getLogger()->log("source must not be empty.",Utils_Logging_Logger::TYPE_ERROR);
                }
                else{ $resource = fopen($this->source,"rb"); }
                if($this->source==""){
                    $dest = false;
                    Utils_Logging_Logger::getLogger()->log("destination must not be empty.",Utils_Logging_Logger::TYPE_ERROR);
                }
                else{ $dest     = fopen($this->dest,"wb"); }
                if($resource!==false&&$dest!==false){
                    while(!feof($resource)){
                        $read = fread($resource,$this->buffer);
                        fwrite($dest,$read,$this->buffer);
                    }
                    chmod($this->dest,0644);
                    fclose($dest); fclose($resource);
                    return true;
                }else{
                     return false;   
                }
            }else{
                return false;
            }
        }
    }
    

提交回复
热议问题