Home > OS >  Is it possible to stream a video file from external server to site using sftp?
Is it possible to stream a video file from external server to site using sftp?

Time:01-05

I'm very new to using ssh and sftp and want to know if it's possible to stream a video from an external server to your site using something like

src="sftp://user:password@server/path/to/file" . (I'm using php for backend).

CodePudding user response:

Yes, it is possible to stream a video file from an external server to your website using SFTP without downloading the file using PHP. Here is one way you can do it:

Connect to the external server using SFTP in PHP. You can use the ssh2_sftp() function for this.

Open the video file on the external server using fopen() and set the mode to "rb" (read binary).

Set the appropriate HTTP headers for the video file. You can use the header() function to do this.

Use the fpassthru() function to read the file and write it to the output buffer.

Close the file using fclose().

Here is some sample code that demonstrates this process:

<?php

// Connect to the external server using SFTP
$sftp = ssh2_sftp('1.2.3.4', 22, 'username', 'password');

// Open the video file on the external server
$file = fopen("ssh2.sftp://$sftp/path/to/video.mp4", 'rb');

// Set the appropriate HTTP headers for the video file
header("Content-Type: video/mp4");
header("Content-Length: " . filesize("ssh2.sftp://$sftp/path/to/video.mp4"));

// Read and write the file to the output buffer
fpassthru($file);

// Close the file
fclose($file);

?>

CodePudding user response:

Did you see this ?

https://codesamplez.com/programming/php-html5-video-streaming-tutorial

I think you can use a sftp URL as video src.

  • Related