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;
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏