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

当浏览器将文件上载到服务器时,它会发送包含文件内容的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, 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);

基本上,你:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值