大家好 我用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" 而不是全部的路径名称。各位大侠有招吗?谢谢了!

解决方案 »

  1.   

    自己搞定了。1. 不要用cURL指定的格式上传文件 自己构建一个文件上传的Body.
    2. 在Header里面把Content-Type改一下。
    如果有这个方面的不懂的,可以详细email me [email protected]
      

  2.   

    事隔多年,但是还是不断有人发邮件询问相关问题,这里作统一回答。1. 解决问题,首先要掌握必要的知识点,那就是Http Request的格式。具体可以参考http://www.ietf.org/rfc/rfc2616.txt. 那么上面的问题就是用CURL来模拟一个Http Post 请求。
    下面是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