Dropbox api到tmp文件

时间:2013-05-24 13:01:49

标签: dropbox tmp

我有一个很难的问题。我想使用DropPHP类从dropbox上传文件。不幸的是,我的共享托管环境无法在Web服务器上写入文件。唯一的选择是使用tmp文件夹或数据库。

由于DropPHP使用自定义函数DownloadFile()将文件下载到Web服务器,因此我必须更改函数以使其写入tmp文件夹。我怎么做??我还不熟悉tmp ......

该功能如下:

public function DownloadFile($dropbox_file, $dest_path='', $rev=null, $progress_changed_callback = null)
    {
        if(is_object($dropbox_file) && !empty($dropbox_file->path))
            $dropbox_file = $dropbox_file->path;

        if(empty($dest_path)) $dest_path = basename($dropbox_file);

        $url = $this->cleanUrl(self::API_CONTENT_URL."/files/$this->rootPath/$dropbox_file");
        $content = (!empty($rev)) ? http_build_query(array('rev' => $rev),'','&') : null;
        $context = $this->createRequestContext($url, "GET", $content);

        $fh = @fopen($dest_path, 'wb'); // write binary
        if($fh === false) {
            @fclose($rh);
            throw new DropboxException("Could not create file $dest_path !");
        }

        if($this->useCurl) {
            curl_setopt($context, CURLOPT_BINARYTRANSFER, true);
            curl_setopt($context, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($context, CURLOPT_FILE, $fh);
            $response_headers = array();
            self::execCurlAndClose($context, $response_headers);
            fclose($fh);
            $meta = self::getMetaFromHeaders($response_headers);
            $bytes_loaded = filesize($dest_path);
        } else {
            $rh = @fopen($url, 'rb', false, $context); // read binary
            if($rh === false)
                throw new DropboxException("HTTP request to $url failed!");



            // get file meta from HTTP header
            $s_meta = stream_get_meta_data($rh);
            $meta = self::getMetaFromHeaders($s_meta['wrapper_data']);
            $bytes_loaded = 0;
            while (!feof($rh)) {
              if(($s=fwrite($fh, fread($rh, self::BUFFER_SIZE))) === false) {
                @fclose($rh);
                @fclose($fh);
                throw new DropboxException("Writing to file $dest_path failed!'");
              }
              $bytes_loaded += $s;
              if(!empty($progress_changed_callback)) {
                call_user_func($progress_changed_callback, $bytes_loaded, $meta->bytes);
              }
            }

            fclose($rh);
            fclose($fh);
        }

        if($meta->bytes != $bytes_loaded)
            throw new DropboxException("Download size mismatch!");

        return $meta;
    }

1 个答案:

答案 0 :(得分:0)

发现它!这是我必须改变的以下一行:

curl_setopt($context, CURLOPT_FILE, $fh); 

如果我删除它,如果我这样做,该文件将作为二进制字符串返回: 替换这个

self::execCurlAndClose($context, $response_headers);

由此

$thefilebinarystring = self::execCurlAndClose($context, $response_headers);

或者如果我想写入tmp文件,请不要删除我上面显示的行,而是用$ temp = tmp file()

替换你之前创建的$ temp文件的$ fh

但是,我还没有更新其余的代码,但这是核心!

感谢您的回答,我自己! ;)