[英]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.