首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Dropbox API直接将文件传输到远程FTP服务器,而无需下载中间文件

使用Dropbox API直接将文件传输到远程FTP服务器,而无需下载中间文件
EN

Stack Overflow用户
提问于 2015-03-06 23:30:36
回答 1查看 1.3K关注 0票数 0

我在Dropbox上有很大的设计文件(高达500MB),我正在构建一个工具,在我们基于PHP的在线项目管理程序中,通过编程将单个文件传输到供应商的FTP服务器。由于文件大小,我不想下载文件到服务器,然后上传文件到FTP服务器,由于速度和存储空间问题。

我可以使用以下Dropbox API调用:

代码语言:javascript
运行
复制
getFile( string $path, resource $outStream, string|null $rev = null )
Downloads a file from Dropbox. The file's contents are written to the given $outStream and the file's metadata is returned.

我猜我可以使用以下PHP命令:

代码语言:javascript
运行
复制
ftp_fput ( resource $ftp_stream , string $remote_file , resource $handle , int $mode [, int $startpos = 0 ] )
Uploads the data from a file pointer to a remote file on the FTP server.

我没有任何文件数据流的经验,所以我不知道如何将两者连接起来。在网上搜索了几个小时后,我想我应该试着在这里提问。

如何连接getFile的$outstream资源和ftp_fput的$ftp_stream资源?

EN

回答 1

Stack Overflow用户

发布于 2015-03-07 10:10:46

花了半天的时间进行实验,最终让它起作用了。该解决方案涉及到使用PHP data://方案在内存中创建一个流,然后倒带该流以将其发送到FTP服务器。这里是它的要点:

代码语言:javascript
运行
复制
// open an FTP connection
$ftp_connection = ftp_connect('ftp.example.com');
ftp_login($ftp_connection,'username','password');

// get the file mime type from Dropbox, to create the correct data stream type
$metadata = $dopbox->getMetadata($file) // $dropbox is authenticated connection to Dropbox Core API; $file is a complete file path in Dropbox
$mime_type = $metadata['mime_type'];

// now open a data stream of that mime type
// for example, for a jpeg file this would be "data://image/jpeg"
$stream = fopen('data://' .mime_type . ',','w+'); // w+ allows both writing and reading
$dropbox->getFile($file,$stream); // loads the file into the data stream
rewind($stream)
ftp_fput($ftp_connection,$remote_filename,$stream,FTP_BINARY); // send the stream to the ftp server

// now close everything
fclose($stream);
ftp_close($ftp_connection);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28902089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档