这很简单,但对我来说这并不简单。我使用PHP 5.6。
这有效:
<form method="post" action="http://RUSRRJIZ3A2WRL.....ML1IU6D9G@host.localhost/api/images/products/88" enctype="multipart/form-data" >
<input type='file' name='image' />
<button type="submit" >send</button>
</form>
但这不是:
$id_product = 88;
$cfile = curl_file_create('bigimage.jpg','image/jpeg','bigimage');
$data = array('image' => $cfile);
$header = array('Content-Type: multipart/form-data');
$url = PS_SHOP_PATH . "api/images/products/$id_product";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.':');
//curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '@'.realpath('bigimage.jpg').";type=jpeg"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if(!$result = curl_exec($ch)) throw new Exception('curl_exec generate an error.');
curl_close($ch);
任何人都可以帮助我吗?也许这可能是一个安全错误?日志中有时仅显示错误500。
答案 0 :(得分:3)
对我来说,我使用:
// envois de la nouvelle image
$url = PS_SHOP_PATH. "/api/images/products/".$product->id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '@'.$image_path . ";type=" . $image_mime));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
感谢您发布您的代码:
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
没有它我的上传失败了ERROR 500。
再次蠢蠢欲动!