1

I'm writing an API and I'm wanting to uploads from a form POST.

this is my html form

<form method="post" action = 'function.php' enctype="multipart/form-data">
<input type = 'file' name = 'pictures' required>
<input type = 'submit'/>

this is my function.php

$uploads_dir = 'img/BL/';
$tmp_name = $_FILES["pictures"]["tmp_name"];
$name = $_FILES["pictures"]["name"];
$asd =  move_uploaded_file($tmp_name, "$uploads_dir/$name");

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.example.com/upload/images.json",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $asd,
  CURLOPT_HTTPHEADER => array(
    "authorization: Basic NDM0MDY1Mzc6TE9sSmV1V003aTg2a2x6Mw==",
    "cache-control: no-cache",
    "content-type: multipart/form-data",
    "postman-token: a88b550a-3544-85b7-ca6b-1ed8cc5cb35c"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
  // echo $name;
}

and response from API is

{
status: "ERROR",
id: null,
message: "must have an image"
}

the problem is images already save but when i try to upload using cURL get response "Harus ada file gambar" (Must have image) the image isnt read/detected when upload

am I going about this the wrong way? Any advice would be most appreciated. Maybe someone can tell me the clue, or whats wrong with my code, thank you

dianeryanto
  • 600
  • 2
  • 5
  • 18
  • is this error is in your code or response from API ? – BSB Sep 25 '17 at 08:02
  • check error from my code, i already success save image to my folder, but when i want to upload the file using curl, the response is => cURL Error #:couldn't open file "../img/BL/1506324698_1.png" – dianeryanto Sep 25 '17 at 08:03
  • Which PHP version you are using ? – BSB Sep 25 '17 at 08:04
  • Use the full path to image. – Tigger Sep 25 '17 at 08:21
  • @Tigger my full path to image is : "../img/BL/1506324698_1.png" and still error sir, i dont know why – dianeryanto Sep 25 '17 at 08:36
  • @BSB PHP : 5.6.30 sir – dianeryanto Sep 25 '17 at 08:37
  • As question shows you want to upload existing local file to remote using curl. Use $body = ['file' => '@'.$target_file1]; Please note: $target_file1 should be full path of file. Ex. $file = '@/path/to/file.png' – BSB Sep 25 '17 at 08:49
  • @BSB ya target_file1 is my full path to image, i already try change $body = ['file' => $file]; to $body = ['file' => '@'.$target_file1]; but no response – dianeryanto Sep 25 '17 at 08:54
  • First try with static path and without child array as $body['file'] = '@/path/to/file.png'; and print response. Its working good for php 5.6 – BSB Sep 25 '17 at 08:57
  • @illuminarch - No, `../img/BL/1506324698_1.png` is a relative path and I strongly believe that is the problem. A full path would look more like `/home/username/www/site/img/BL/1506324698_1.png`. – Tigger Sep 25 '17 at 08:59
  • I have executed it, its giving response as 1 (true). This is may be permission related issue on your local. – BSB Sep 25 '17 at 09:28
  • @BSB ya me too, the response just 1, its like echo result from my post file function, not response from api can you suggest me with fix the permission? or another way to upload file using php curl ? – dianeryanto Sep 25 '17 at 09:30
  • @illuminarch API is giving response as 1. There is no other response from API for any combination of data. In this case you have to check on local if everything okey on local thn contact API provider. – BSB Sep 25 '17 at 09:45
  • @BSB im already try with another way, and now get response from API. the new problem is the image has been save but when try to upload itu using curl the file is not detected – dianeryanto Sep 25 '17 at 09:49
  • @illuminarch Check the [PHP manual](http://php.net/manual/en/function.curl-setopt.php) at the `CURLOPT_POSTFIELDS` section. Its says, just as other have said, _To post a file, prepend a filename with @ and use the full path_ and more. – Tigger Sep 25 '17 at 09:59
  • @Tigger like this sir : file=@img/BL/KS-P8MY-WH_Front.jpg ?? file= <=is format from API documentation – dianeryanto Sep 25 '17 at 10:10
  • Possible duplicate of [how to upload file using curl with php](https://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php) and [File uploading through curl in php](https://stackoverflow.com/questions/34265999/file-uploading-through-curl-in-php) – Tigger Sep 25 '17 at 10:20
  • thankyou for ur help sir, problem solved @Tigger – dianeryanto Sep 26 '17 at 00:59

0 Answers0