How can I fetch a remote file in php over ssh and return file directly to the browser response without creating a copy of the file on the webserver

Viewed 7646

I am currently using a similar version of the below code to transfer a file from a remote server to my web server and then redirecting to the web server copy of the file in a publicly accessible web location.

$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' ) ;

I also get some files from amazon s3 using their api. When i use this method the api returns the file as an object so I am able to send it directly to the browser with the appropriate headers. like below example.

// Display the object in the browser
header("Content-Type: {$result['ContentType']}");
header("Content-Type: audio/wav");
echo $result['Body'];
}

My question is how can stream/get a file from a remote server and send it to the browser in the same way as the bottom version without creating a physical copy on the webserver. Many thanks in advance

2 Answers
Related