加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 18:19:52 所属栏目:PHP教程 来源:网络整理
导读:使用html表单,我们可以使用enctype =“multipart / form-data”,输入类型=“文件”等将文件从客户端上传到服务器. 有没有办法让文件已经在服务器上并以同样的方式传输到另一台服务器? 谢谢你的提示. // 哇!这是我见过的最快的问答页面!! 当浏览器将文件上
使用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>发布一些数据,包括文件 – 在文件路径前注意@.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读