php – 从服务器到服务器的http传输文件

使用html表单,我们可以使用enctype =“multipart / form-data”,输入类型=“文件”等将文件从客户端上传到服务器.

有没有办法让文件已经在服务器上并以同样的方式传输到另一台服务器?

谢谢你的提示.

// 哇!这是我见过的最快的问答页面!!

当浏览器将文件上载到服务器时,它会发送包含文件内容的HTTP POST请求.

你必须复制它.

使用PHP,最简单(或至少,最常用)的解决方案可能适用于curl.

如果您查看可以使用curl_setopt设置的选项列表,您将看到以下选项:CURLOPT_POSTFIELDS(引用):

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&para2=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.

没有经过测试,但我想这样的事情应该可以解决 – 或者至少帮助你开始:

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

基本上,你:

>正在使用卷曲>必须设置目标网址>表示您希望curl_exec返回结果,而不是输出结果>正在使用POST,而不是GET>发布一些数据,包括文件 – 在文件路径前注意@.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...