如何将图像移动到另一台PHP服务器

如何将图像移动到另一台PHP服务器,php,curl,Php,Curl,我有两台服务器,所以当我使用移动上传文件功能从第一台服务器上传文件/图像时, 文件应该上传到第二个服务器 $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '@/path/to/file.txt')); curl_setopt($ch, CURLOPT_URL, 'http://server2/upload.php');

我有两台服务器,所以当我使用移动上传文件功能从第一台服务器上传文件/图像时, 文件应该上传到第二个服务器

$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '@/path/to/file.txt'));
curl_setopt($ch, CURLOPT_URL, 'http://server2/upload.php');
curl_exec($ch);
curl_close($ch);

但我想知道,第二台服务器upload.php中应该有什么代码,我如何提到path?

首先,不要尝试使用@method上传文件,它在PHP5.5中被弃用,在PHP5.6中默认禁用,在PHP7.0中被完全删除。用于上载文件。要提到路径,只需将路径作为另一个post变量添加到数组中。至于接收器,请阅读php.net上的。最后,考虑如果你想让任何发现URL的人能够上传任何文件。可能不会,您可能想在上传时设置密码,例如

curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    'file' => new CURLFile('path/to/file.jpg'),
    'path' => '/where/ever/you/want.jpg',
    'password' => 'd540cyLp419rdwelv8c-'
));
关于发送者和

if (! hash_equals(hash('sha256', 'd540cyLp419rdwelv8c-', true), hash('sha256', (string) ($_POST['password'] ?? ''), true))) {
    http_response_code(403); // forbidden
    die("wrong/missing password.");
}
$upload_path = $_FILES["file"]["tmp_name"];
$requested_file_path = $_POST['path'];
关于接收方,请参阅前面提到的php文章中的处理文件上传,以了解更多信息