我目前正在使用以下代码的类似版本将文件从远程服务器传输到我的 Web 服务器,然后重定向到可公开访问的 Web 位置中的文件的 Web 服务器副本。
$tempfile = "/mylocalfolder/tempfile.wav"
if (file_exists($tempfile)) {
unlink($tempfile);
}
$selectedfile = htmlspecialchars($_GET["File"]);
$filelink = '/myremotefolder/'.$selectedfile;
$connection = ssh2_connect($remote_server_ip, 22);
ssh2_auth_password($connection, 'username', 'password');
//echo $filelink.','. $tempfile;
ssh2_scp_recv($connection, $filelink, "/mylocalfolder/tempfile.wav");
header( 'Location: /mylocalfolder/recording.wav' ) ;
Run Code Online (Sandbox Code Playgroud)
我还使用他们的 api 从 amazon s3 获取一些文件。当我使用此方法时,API 将文件作为对象返回,这样我就可以使用适当的标头将其直接发送到浏览器。就像下面的例子。
// Display the object in the browser
header("Content-Type: {$result['ContentType']}");
header("Content-Type: audio/wav");
echo $result['Body'];
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何从远程服务器流式传输/获取文件并将其以与底层版本相同的方式发送到浏览器,而无需在网络服务器上创建物理副本。提前谢谢了
您可以使用 ssh2_sftp http://php.net/manual/en/function.ssh2-sftp.php ...您必须安装 ssh2 绑定作为 PECL 扩展(http://php.net/manual/es/book.ssh2 .php )
示例代码可能是...
$sftp = ssh2_sftp($connection);
$remote = fopen("ssh2.sftp://$sftp/path/to/file", 'rb');
header( 'Content-type: ......');
while(!feof($remote)){
echo( fread($remote, 4096));
}
Run Code Online (Sandbox Code Playgroud)
我还没有测试过代码,但它应该可以工作。