How to upload image to another server?

瘦欲@ 提交于 2019-12-09 06:27:33

问题


I want to create an application server that serves html content which contains links to static images served by another server on a different domain. The images are uploaded by users through the application server.

This is what I would do to upload a JPEG file to the application server:

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

How would the code be changed if I were to upload the file to another server instead?

Add Note: The folders are to be created on the fly if it doesn't exist.


回答1:


Mostly depends on what you can use.

You can do it with secure SFTP:

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

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

PHP manual here: function.ssh2-scp-send.php

Or unsecure 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 manual here: function.ftp-put.php

Or you could send HTTP request using PHP:

This is more like real web browser behavior as seen by another server:

You can use socket_connect(); and socket_write();, I will add more information about those later.



来源:https://stackoverflow.com/questions/10274551/how-to-upload-image-to-another-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!