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

File upload not working with PHP curl method (https://developer.box.com/v2.0/reference#upload-a-file

Answered
New post

Comments

5 comments

  • vijeeshciber

    Hello Everyone,

     

    I have find out the solution for this issue. Following is my working code with PHP CURL.

     

    $url = 'https://upload.box.com/api/2.0/files/content';
    $json = json_encode(array(
    'name' => $file->filename,
    'parent' => array('id' => $folder)
    ));
    $params = array(
    'attributes' => $json,
    'file'=>new CurlFile(drupal_realpath($file->uri),$file->filemime,$file->filename)
    );
    $headers = array("Authorization: Bearer ".$res_data['access_token']);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    $response = curl_exec($ch);
    $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $errNo = curl_errno($ch);
    $errStr = curl_error($ch);
    curl_close($ch);

     

    0
    Comment actions Permalink
  • Dipak384

    Can please give me this error solution.

    Screenshot_1.png

    0
    Comment actions Permalink
  • bala33

    Hello ,are you got any solutions for this issue.

    0
    Comment actions Permalink
  • elii

    Hi !

     

    Welcome to the Box Community and thank you for your first post!

     

    Did the solution provided by the original poster not work for you? If not, can you give us more details on the kinds of issues you're facing? This will help other community members understand how to help you best!

     

    Thank you again for posting, and hope to hear back from you soon!

    Emma

    Box Community Management Intern

    0
    Comment actions Permalink
  • bala33

    Hi, Thanks,

     

    ya, it's working below I provide code.

    $pathoffile=$_FILES['boxfile']['tmp_name'];
    $filename=$_FILES['boxfile']['name'];
    $handle = fopen($pathoffile, "r");
    $data = fread($handle, filesize($pathoffile));

    $tokens = array();
    $tokens[] = "tokenkey";
    $serverKey ="Bearer tokenkey";
    $url = 'https://upload.box.com/api/2.0/files/content';

    $attributes = array('name' => $filename, 'parent' => array('id' => '0'));
    $params = array('attributes' => json_encode($attributes));
    $headers = array();
    $headers[] = 'Content-Type: multipart/form-data';
    $headers[] = 'Authorization: key='.$serverKey;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    'file' => curl_file_create($pathoffile),
    'attributes' => json_encode($attributes)

    ));
    $response = curl_exec($ch);
    $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

     

     

     

    0
    Comment actions Permalink

Please sign in to leave a comment.