如何将图片上传到另一个服务器?

5
我希望创建一个应用程序服务器,它提供包含指向不同域上的另一个服务器提供的静态图像链接的HTML内容。这些图像是用户通过应用程序服务器上传的。
以下是上传JPEG文件到应用程序服务器的步骤:
if(!file_exists("folder_name")) mkdir("folder_name", 0770);
$temp_file = $_FILES['image']['tmp_name'];
$im = imagecreatefromjpeg($temp_file);
$destination = "folder_name/file_name.jpg";
imagejpeg($im, $destination);
imagedestroy($im);

如果我要将文件上传到另一个服务器,代码会如何更改?
添加注释:如果不存在,则应动态创建文件夹。
1个回答

18

主要取决于您可以使用什么。

您可以通过安全的SFTP实现:

$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);

这里是PHP手册:function.ssh2-scp-send.php

或者使用不安全的FTP:

$file = 'somefile.txt';
$remote_file = 'readme.txt';

// set up basic connection
$conn_id = ftp_connect("ftp.example.com");

// login with username and password
$login_result = ftp_login($conn_id, "username", "password");

// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
 echo "successfully uploaded $file\n";
} else {
 echo "There was a problem while uploading $file\n";
}

// close the connection
ftp_close($conn_id);

PHP手册在这里:function.ftp-put.php

或者你可以使用PHP发送HTTP请求:

这更像是另一个服务器看到的真实的Web浏览器行为:

您可以使用socket_connect();socket_write();,我稍后会添加有关它们的更多信息。


+1 谢谢。我认为这些函数非常有用。让我好好看看每一个,然后再回复你。 - Question Overflow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接