0

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.

私はファイル データ ストリームの経験がないので、この 2 つを接続する方法がわかりません。数時間オンラインで検索した後、ここで質問してみようと思いました。

getFile の $outstream リソースを ftp_fput の $ftp_stream リソースに接続するにはどうすればよいですか?

4

1 に答える 1

0

これを試すのに半日を費やし、ついにそれが機能するようになりました。この解決策では、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);
于 2015-03-07T02:10:46.017 に答える