Dropbox api to tmp files



我有一个相当难的问题。我想从dropbox上传文件,使用DropPHP类。不幸的是,我的共享主机环境不能在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;
    }

找到了!我必须修改的是下面这行:

curl_setopt($context, CURLOPT_FILE, $fh); 

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

self::execCurlAndClose($context, $response_headers);
由这个

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

或者如果我想写入tmp文件,不要删除上面显示的行,而是用您之前创建的$temp文件替换$fh,使用$temp = tmp file()

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

谢谢你的精彩回答,我自己!div;)

相关内容

最新更新