duanou9739 2015-03-06 15:30
浏览 97

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

I have large design files (up to 500 MB) on Dropbox, and I'm building a tool to transfer a single file programmatically to a vendor's FTP server in our online PHP-based project management program. Because of the file size, I don't want to download the file to the server, then upload that file to the FTP server, due to both speed and storage space issues.

I can use the following Dropbox API call:

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.

And I'm guessing I can use the following PHP command:

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.

I don't have any experience with file data streams, so I have no idea how to connect the two. After a couple of hours of online searching, I figured I'd try asking here.

How do I connect getFile's $outstream resource with ftp_fput's $ftp_stream resource?

  • 写回答

1条回答 默认 最新

  • dongyuntao2000 2015-03-07 02:10
    关注

    Spent half the day experimenting with this, and finally got it to work. The solution involves using the PHP data:// scheme to create a stream in memory, then rewinding that stream to send it to the FTP server. Here's the gist of it:

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

报告相同问题?

悬赏问题

  • ¥15 把Excel导入MATLAB显示错误怎么解决?
  • ¥15 Java中消息和缓存如何使用
  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx