【问题标题】:Transfer file using Dropbox API directly to remote FTP server without downloading intermediate file使用 Dropbox API 将文件直接传输到远程 FTP 服务器,无需下载中间文件
【发布时间】:2015-03-07 10:10:46
【问题描述】:

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

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

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 命令:

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 资源连接起来?

【问题讨论】:

标签: php ftp dropbox data-stream


【解决方案1】:

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

// 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);

【讨论】:

  • 这对我不起作用。我收到错误:PHP 致命错误:未捕获的异常 'InvalidArgumentException' 带有消息 ''outStream' 类型错误;期待资源,得到布尔值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
  • 2011-08-15
  • 1970-01-01
相关资源
最近更新 更多