David de Barros
David de Barros

Reputation: 21

Prestashop API upload image error PHP 5.6

It's simple, but for me this is not simple. I use PHP 5.6.

This works:

<form method="post" action="http://[email protected]/api/images/products/88" enctype="multipart/form-data"  >
<input type='file' name='image' />
<button type="submit" >send</button>
</form>

But this not:

$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);

Can anyone help me? Maybe it can be a security error? Only error 500 is sometimes shown in log.

Upvotes: 0

Views: 1489

Answers (1)

Fabien Mourioux
Fabien Mourioux

Reputation: 31

For me I use :

    // 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);       

Thanks for posting your code :

  curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);

Without it my upload fails with ERROR 500.

Thnaks again !

Upvotes: 3

Related Questions