我有一个相当难的问题。我想使用 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;
}