[英]php image upload using curl request
我正在嘗試通過imageUploader.php將圖像文件上傳到服務器。 我需要從我的角度腳本和php腳本中上傳圖像。 我的角度http請求成功上傳了圖像,但是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 );
}
問題是,如果我在php腳本中刪除了multipart / form-data部分,則圖像已成功上傳,但是當我在服務器中查看該圖像時,將無法查看圖像內容。 就像在上傳圖像時圖像已損壞。
試試這個代碼,它將幫助您使用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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.