PHP Extension Dropbox: Dropbox Create Folder

Back to Index

Create a folder. Creates the folder /Halloween/emojis

Documentation: https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder

CURL Command

curl -X POST https://api.dropboxapi.com/2/files/create_folder_v2 \
    --header "Authorization: Bearer DROPBOX-ACCESS-TOKEN" \
    --header "Content-Type: application/json" \
    --data "{\"path\": \"/Halloween/emojis\",\"autorename\": false}"

PHP Extension Example

<?php

// The version number (9_5_0) should match version of the Chilkat extension used, omitting the micro-version number.
// For example, if using Chilkat v9.5.0.48, then include as shown here:
include("chilkat_9_5_0.php");

$rest = new CkRest();

//  URL: https://api.dropboxapi.com/2/files/create_folder_v2
$bTls = true;
$port = 443;
$bAutoReconnect = true;
$success = $rest->Connect('api.dropboxapi.com',$port,$bTls,$bAutoReconnect);
if ($success != true) {
    print 'ConnectFailReason: ' . $rest->get_ConnectFailReason() . "\n";
    print $rest->lastErrorText() . "\n";
    exit;
}

//  See the Online Tool for Generating JSON Creation Code
$json = new CkJsonObject();
$json->UpdateString('path','/Halloween/emojis');
$json->UpdateBool('autorename',false);

$rest->AddHeader('Authorization','Bearer DROPBOX-ACCESS-TOKEN');
$rest->AddHeader('Content-Type','application/json');

$sbRequestBody = new CkStringBuilder();
$json->EmitSb($sbRequestBody);
$sbResponseBody = new CkStringBuilder();
$success = $rest->FullRequestSb('POST','/2/files/create_folder_v2',$sbRequestBody,$sbResponseBody);
if ($success != true) {
    print $rest->lastErrorText() . "\n";
    exit;
}

$respStatusCode = $rest->get_ResponseStatusCode();
if ($respStatusCode >= 400) {
    print 'Response Status Code = ' . $respStatusCode . "\n";
    print 'Response Header:' . "\n";
    print $rest->responseHeader() . "\n";
    print 'Response Body:' . "\n";
    print $sbResponseBody->getAsString() . "\n";
    exit;
}

$jsonResponse = new CkJsonObject();
$jsonResponse->LoadSb($sbResponseBody);

//  See the Online Tool for Generating JSON Parse Code

$metadataName = $jsonResponse->stringOf('metadata.name');
$metadataId = $jsonResponse->stringOf('metadata.id');
$metadataPath_lower = $jsonResponse->stringOf('metadata.path_lower');
$metadataPath_display = $jsonResponse->stringOf('metadata.path_display');
$metadataSharing_infoRead_only = $jsonResponse->BoolOf('metadata.sharing_info.read_only');
$metadataSharing_infoParent_shared_folder_id = $jsonResponse->stringOf('metadata.sharing_info.parent_shared_folder_id');
$metadataSharing_infoTraverse_only = $jsonResponse->BoolOf('metadata.sharing_info.traverse_only');
$metadataSharing_infoNo_access = $jsonResponse->BoolOf('metadata.sharing_info.no_access');
$i = 0;
$count_i = $jsonResponse->SizeOfArray('metadata.property_groups');
while ($i < $count_i) {
    $jsonResponse->put_I($i);
    $template_id = $jsonResponse->stringOf('metadata.property_groups[i].template_id');
    $j = 0;
    $count_j = $jsonResponse->SizeOfArray('metadata.property_groups[i].fields');
    while ($j < $count_j) {
        $jsonResponse->put_J($j);
        $name = $jsonResponse->stringOf('metadata.property_groups[i].fields[j].name');
        $value = $jsonResponse->stringOf('metadata.property_groups[i].fields[j].value');
        $j = $j + 1;
    }

    $i = $i + 1;
}


?>

Sample JSON Response Body

{
  "metadata": {
    "name": "emojis",
    "id": "id:a4ayc_80_OEAAAAAAAAAXz",
    "path_lower": "/halloween/emojis",
    "path_display": "/Halloween/emojis",
    "sharing_info": {
      "read_only": false,
      "parent_shared_folder_id": "84528192421",
      "traverse_only": false,
      "no_access": false
    },
    "property_groups": [
      {
        "template_id": "ptid:1a5n2i6d3OYEAAAAAAAAAYa",
        "fields": [
          {
            "name": "Security Policy",
            "value": "Confidential"
          }
        ]
      }
    ]
  }
}