Level up your Box knowledge with brand new learning paths on Box University. Visit training.box.com to get started

File upload using BOX API

Answered
New post

Comments

3 comments

  • Jason

    Hi  , 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:


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

     

    0
    Comment actions Permalink
  • kishimoto2

    Dear .

     

    thank you for your answer.
    It was very helpful

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

    0
    Comment actions Permalink
  • kishimoto2

    Dear .

     

    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.

    0
    Comment actions Permalink

Please sign in to leave a comment.