0

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

由于 DropPHP 使用自定义函数 DownloadFile() 将文件下载到网络服务器,因此我必须更改函数以使其写入 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;
    }
4

1 回答 1

0

找到了!这是我必须更改的以下行:

curl_setopt($context, CURLOPT_FILE, $fh); 

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

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

这样

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

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

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

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

于 2013-05-25T20:49:45.477 回答