dsomm80482 2011-03-19 03:07
浏览 1586

如何在CURL下载文件时使用CURLOPT_WRITEFUNCTION

My Class for download file direct from a link:

MyClass{

          function download($link){
                ......
                $ch = curl_init($link);
                curl_setopt($ch, CURLOPT_FILE, $File->handle);
                curl_setopt($ch,CURLOPT_WRITEFUNCTION , array($this,'__writeFunction'));
                curl_exec($ch);
                curl_close($ch);
                $File->close();
                ......

            }

          function __writeFunction($curl, $data) {
                return strlen($data);            
          } 
}

I want know how to use CRULOPT_WRITEFUNCTION when download file. Above code if i remove line:

curl_setopt($ch,CURLOPT_WRITEFUNCTION , array($this,'__writeFunction'));

Then it will run good, i can download that file.But if i use CURL_WRITEFUNCTION option i can't download file.

  • 写回答

4条回答 默认 最新

  • dongyakui8675 2011-03-19 03:18
    关注

    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.

    <?php
    class Utils_FileDownload  {
        private $source;
        private $dest;
        private $buffer;
        private $overwrite;
        public function __construct($source,$dest,$buffer=4096,$overwrite=false){
            $this->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;
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码