微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>发布一些数据,包括文件 – 在文件路径前注意@.

原文地址:https://www.jb51.cc/php/135416.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐