大家好 我用curl在服务器上 上传文件到远程服务器上面,文件能够正常上传,但是远程主机拿到的文件名不正确,代码如下: <?php
/* http://localhost/upload.php:
print_r($_POST);
print_r($_FILES);
*/
$ch = curl_init();
$data = array('name' => 'Foo', 'file' => '@/home/autouvl/public_html/asmallorange/log.txt');
curl_setopt($ch, CURLOPT_URL, 'http://www.test.com/test/receivefile.aspx');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
echo(curl_exec($ch));
?> 在这个例子中 远程主机拿到的文件名为"/home/autouvl/public_html/asmallorange/log.txt" 但是我想要的是 "log.txt" 而不是全部的路径名称。各位大侠有招吗?谢谢了!
/* http://localhost/upload.php:
print_r($_POST);
print_r($_FILES);
*/
$ch = curl_init();
$data = array('name' => 'Foo', 'file' => '@/home/autouvl/public_html/asmallorange/log.txt');
curl_setopt($ch, CURLOPT_URL, 'http://www.test.com/test/receivefile.aspx');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
echo(curl_exec($ch));
?> 在这个例子中 远程主机拿到的文件名为"/home/autouvl/public_html/asmallorange/log.txt" 但是我想要的是 "log.txt" 而不是全部的路径名称。各位大侠有招吗?谢谢了!
2. 在Header里面把Content-Type改一下。
如果有这个方面的不懂的,可以详细email me [email protected]
下面是2个关键步骤。
2. 在Request的头里面设置boundary.$headers = array("Content-Type:multipart/form-data; boundary={$this->boundary}");
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers); 2. 构造上传的Boday.$row = array();
$row[] = "--{$this->boundary}";
$row[] = "Content-Disposition: form-data; name=\"".$val->Name."\"; filename=\"".$val->FileName."\"";
$contentType = "application/octet-stream";
$row[] = "Content-Type:$contentType\r\n";
$row[] = $val->fileContent;//the real file content.
$pData[] = join( "\r\n", $row );
$pData[] = "--{$this->boundary}--";
$this->postData = join("\r\n", $pData);curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->postData);
//and finally send curl request
$this->response = curl_exec($this->ch);
以上只是手写部分代码片段,介绍的是大致思路,你需要先理解,然后再移植到你的环境中。thanks,Sean