是否可以通过Html-Fom将媒体文件(图像/视频/pdf等)从服务器发送到另一台服务器


Is it feasible to send media files(images/ video/ pdf etc...) from a server to another server through Html Fom?

情况是:如何通过服务器A中的Html表单上传文件,上传的文件应该发送到服务器B

我阅读了与此主题相关的答案,但它只允许用post方法发送数据。

  1. HTML/PHP将方法发布到不同的服务器

  2. Ajax POST到另一台服务器-克服跨域限制

一些答案建议使用ftp_fput()函数,这是有风险的,因为您的凭据将在线且可访问。(您应该使用ftp_login ( resource $ftp_stream , string $username , string $password )

1。使用ftp(确保使用加密连接,而不是纯ftp)和scp,在scp中可以使用ssh公钥身份验证,这与存储mysql密码一样安全,只需确保凭据不可访问即可。无论如何,您都需要任何类型的身份验证(也适用于html.php)

1a。Rsync+crontab难道不可能使用某种cronjob和rsync来完成任务吗?

2.要接收,请发送带有卷曲的文件

<?php
$url = 'http://target-server/accept.php';
//This needs to be the full path to the file you want to send.
$file = realpath('./sample.jpeg');
// the post fields.
// note the "@", to denote that the file path should be evaluated
$post = array(
    'extra_post_field' => '123456',
    'file_contents' => '@' . $file
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// send the request & close the connection
$result = curl_exec($ch);
curl_close($ch);
// result is the response, this can also be a json response for example
echo $result;
?>

2b。接收文件(注意:此示例需要一个安全/身份验证层)

<?php
// make sure targetFolder is writable by the webserver
$targetFolder = '/your/uploaded/files/folder';
// This will be the target file
$targetFile = $targetFolder . basename($_FILES['file_contents']['name']);
// do your authentication + validation here
echo '<pre>';
if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $targetFile)) {
    echo "File is valid, and was successfully uploaded.'n";
} else {
    echo "Something went wrong uploading the file";
}