File upload using BOX API

SOLVED
Go to solution
Highlighted
Occasional Contributor

File upload using BOX API

Using BOX API in PHP program
I want to upload a file.

But it doesn't work.
In the response, the message "The request was rejected because no multipart boundary was found" is returned.

Please tell me the possible causes.
The sample code below does not work.

 

 

$request = $_REQUEST->all();
$file_obj = $ request->file('file');
$originalFileName = $file_obj->getClientOriginalName();
$uploadFileName = $file_obj->getFileName();
$uploadFileFull = $file_obj->getPathName();
$uploadFileSize = $file_obj->getSize();

 

$param_attributes = array('name' => $originalFileName, 'parent' => array('id' => '0'));
$param_file = "@".$originalFileName;
$param = array('attributes' => $param_attributes, 'file' => $param_file);

$param_json = json_encode($param);

 

$header = array("Authorization:Bearer { 'access_token' }", "Content-Type:multipart / form-data");

$ch = curl _init( "https://api.box.com/2.0/files/content" );
cur_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $param_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);

 

3 REPLIES 3
Highlighted
Box Employee

Re: File upload using BOX API

Hi @kishimoto2 , hope you are doing well.

 

The error may be in how you are attaching the file content to the curl request. I'm not sure what this does:

 

$param_file = "@".$originalFileName;

 

But one thing you can try is using the CURLFILE class:

https://www.php.net/manual/en/class.curlfile.php

 

Here is a working example on PHP 7.3:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "https://upload.box.com/api/2.0/files/content",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array('attributes' => '{"name":"test.test", "parent": {"id": "0"}}','file'=> new CURLFILE('/path/to/file')),
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $token",
),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

 

Highlighted
Occasional Contributor

Re: File upload using BOX API

Dear @Jason62.

 

thank you for your answer.
It was very helpful

I'll try referring to the code that I was told.
Thank you very much.

Highlighted
Occasional Contributor

Re: File upload using BOX API

Dear @Jason62.

 

There was a problem with the upload file specification.
By using the CURLFile object you taught me,
The problem is solved.

 

// Mistake part
$param_attributes = array('name' => $originalFileName,'parent' => array('id' => '0'));
$param_file = "@".$originalFileName;
$param = array('attributes' => $param_attributes,'file' => $param_file);

$param_json = json_encode($param);


// Modified part
$param_attributes = array('name' => $originalFileName,'parent'=> array('id' => '0'))
$param_file = new \CURLFile($uploadFileFull);
$param_json = array(
    'attributes' => json_encode($param_attributes)
    'file' => $param_file
);


Thank you very much.