php 用php上传文件到另一个php服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3135787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload file with php to another php server
提问by Tominator
I'm not asking about uploading a file from a browser to a php script, there's plenty of tutorials about that already. I'm asking about this:
我不是在问将文件从浏览器上传到 php 脚本,已经有很多关于这方面的教程。我在问这个:
I have a php script that has accepted a file from the user, and the file is currently on the hard disk of server 1. I want to upload the file from server 1 to a php script on server 2, using the regular Http post protocol, so the php script on server 2 can be written as a standard file-upload handler.
我有一个 php 脚本,它已经接受了来自用户的文件,该文件当前在服务器 1 的硬盘上。我想使用常规的 Http post 协议将文件从服务器 1 上传到服务器 2 上的 php 脚本,因此服务器 2 上的 php 脚本可以编写为标准文件上传处理程序。
I cannot find any tutorial on the internet, because they all talk about browser->server1. The tutorials about php upload all talk about ftp, but I don't want to use that protocol.
我在互联网上找不到任何教程,因为他们都在谈论浏览器-> server1。php upload 的教程都讲ftp,但是我不想用那个协议。
Please help?
请帮忙?
回答by Daniel Egeberg
You can use CURL for this. Something like this should do it.
您可以为此使用 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');
curl_exec($ch);
curl_close($ch);
You can then handle the the server2 part as a regular file upload. See curl_setopt()for more information on those options.
然后,您可以将 server2 部分作为常规文件上传进行处理。有关curl_setopt()这些选项的更多信息,请参见。
回答by bkulyk
You could use SOAP to send the file from one server to the other.
您可以使用 SOAP 将文件从一台服务器发送到另一台服务器。
Receiving server:
接收服务器:
<?php
$server = new Soap_Server( null, array('uri'=>'somerui') );
$server->addFunction( 'receiveFile' );
function receiveFile( $file ) {
file_put_contents( 'somepath', base64_decode( $file ) );
}
?>
Sending server:
发送服务器:
<?php
$client = new Soap_Client( null, array('uri'=>'somerui') );
$client->receiveFile( base64_encode( file_get_contents( 'somepath' ) );
?>
回答by Borik
You need two Scripts.
你需要两个脚本。
First script that will in way emulate browser behavior, it will take a file and send it to seconds script, that will handles it just like regular file upload script.
第一个脚本将模拟浏览器行为,它将获取一个文件并将其发送到秒脚本,该脚本将像常规文件上传脚本一样处理它。
My guess you have to use "http_post_fields" for the first script, it seems to handle files. http://us2.php.net/manual/en/function.http-post-fields.php
我猜你必须对第一个脚本使用“http_post_fields”,它似乎处理文件。 http://us2.php.net/manual/en/function.http-post-fields.php
Good Luck.
祝你好运。

