2016-07-06 178 views
0

我試圖通過上傳一個圖像文件到服務器imageUploader.php。我需要從角度腳本和php腳本上傳圖片。我的角度http請求成功上傳圖像,但PHP腳本無法上傳圖像文件。使用curl請求php圖像上傳

這是角請求:

fd = new FormData(); 
fd.append('file', file); 

    $http 
    .post(uUrl, fd, { 
     transformRequest: angular.identity, 
     headers: { 
      'Content-Type': "multipart/form-data" 
     } 
    }) 

,這是PHP請求

$data['file'] = new CurlFile($data_string['tmp_name'], $data_string['type']); 

    if (! isset ($this->conType)) 
     $this->conType = 'multipart/form-data'; 
    $ch = curl_init ($this->baseUrl ); 
    curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
    curl_setopt ($ch, CURLOPT_POST, 1); 
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $data); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    $this->addHeader ('Content-Type', $this->conType); 
    curl_setopt ($ch, CURLOPT_HTTPHEADER, $this->headerArray); 
    $result = curl_exec ($ch); 

    public function addHeader($k, $v) { 
     array_push ($this->headerArray, $k . ": " . $v); 
    } 

的事情是,如果刪除的multipart/form-data的在PHP腳本部分,圖像成功上傳,但是當我在服務器中查看圖像時,無法查看圖像內容。這就像上傳圖像時圖像已損壞。

回答

0

試試這個代碼,它會幫助你使用curl

$ch = curl_init(); 
//$data = array('name' => 'Foo', 'file' => '@/path/to/image.jpeg'); 
$data['file'] = new CurlFile($data_string['tmp_name'], $data_string['type']); 
curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php'); 
curl_setopt($ch, CURLOPT_POST, 1); 
//CURLOPT_SAFE_UPLOAD defaulted to true in 5.6.0 
//So next line is required as of php >= 5.6.0 
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_exec($ch); 
+0

到uploadfile它給的我這個錯誤**的@filename API文件上傳的用法已過時。請改用CURLFile類** –