http传输文件从服务器到服务器 [英] http transfer file from server to server
问题描述
有没有办法让文件已经在服务器上,并以相同的方式将其传输到另一个服务器?
感谢提示。
//哇!这是我见过的最快的问题解答页面!
当浏览器上传文件到服务器时,发送一个包含文件内容的HTTP POST请求。
您将有te复制。
使用PHP,最简单的(或至少最常用的)解决方案可能与 curl
。 你看看选项列表中你可以用 curl_setopt $设置C $ C>
,你会看到这样一句: CURLOPT_POSTFIELDS
(报价):
要在HTTPPOST
操作中发布的完整数据。
要发布文件,
预先使用@指定文件名,并使用
完整路径。结果,这可以是
像
A urlencoded的字符串传递 'PARA1 = VAL1&安培; PARA2 = val2的&安培; ...' 或作为
阵列与现场名作为关键字和
字段数据作为值。如果value是
数组,则Content-Type头将
设置为multipart / form-data。
未经测试,但我想这样的事情应该可以做到 - 或者至少可以帮助您开始:
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,http://www.example.com/your-destination-script.php);
curl_setopt($ ch,CURLOPT_HEADER,false);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_POST,true);
curl_setopt($ CH,CURLOPT_POSTFIELDS,数组(
'文件'=> @ / ... / file.jpg',
//你要改名字,在这里,我想
//一些其他字段?
));
$ result = curl_exec($ ch);
curl_close($ ch);
基本上,您:
curl_exec
POST ,而不是 GET <$ c $>来返回结果,而不是输出结果
@
li>
with html forms we can upload a file from a client to a server with enctype="multipart/form-data", input type="file" and so on.
Is there a way to have a file already ON the server and transfer it to another server the same way?
Thanks for hints.
// WoW! This is the fastest question answering page i have ever seen!!
When the browser is uploading a file to the server, it sends an HTTP POST request, that contains the file's content.
You'll have te replicate that.
With PHP, the simplest (or, at least, most used) solution is probably to work with curl
.
If you take a look at the list of options you can set with curl_setopt
, you'll see this one : CURLOPT_POSTFIELDS
(quoting) :
The full data to post in a HTTP "POST" operation.
To post a file, prepend a filename with @ and use the full path.
This can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' or as an array with the field name as key and field data as value.
If value is an array, the Content-Type header will be set to multipart/form-data.
Not tested, but I suppose that something like this should do the trick -- or, at least, help you get started :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/your-destination-script.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'file' => '@/..../file.jpg',
// you'll have to change the name, here, I suppose
// some other fields ?
));
$result = curl_exec($ch);
curl_close($ch);
Basically, you :
- are using curl
- have to set the destination URL
- indicate you want
curl_exec
to return the result, and not output it - are using
POST
, and notGET
- are posting some data, including a file -- note the
@
before the file's path.
这篇关于http传输文件从服务器到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!