问题描述
使用html表单我们可以将文件从客户端上载到enctype =multipart / form-data,输入类型=文件等等的服务器上。有没有办法让文件已经在服务器上,并以相同的方式将其传输到另一个服务器?
感谢提示。
//哇!这是我见过的最快的问题解答页面!
当浏览器上传文件到服务器时,发送一个包含文件内容的HTTP POST请求。
您将有te复制。
使用PHP,最简单的(或至少最常用的)解决方案可能与。 你看看选项列表中你可以用,你会看到这样一句: CURLOPT_POSTFIELDS
(报价):
未经测试,但我想这样的事情应该可以做到 - 或者至少可以帮助您开始:
$ 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
- 必须设置目标URL
- 表明您需要
curl_exec
POST ,而不是GET <$ c $>来返回结果,而不是输出结果
/ code>
- 发布一些数据,包括一个文件 - 在文件路径之前记下
@
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) :
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 not GET
- are posting some data, including a file -- note the
@
before the file's path.
这篇关于http传输文件从服务器到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!