PHP Extension Dropbox: Dropbox Copy a File or Folder

Back to Index

Copy a file or folder to a different location in the user's Dropbox. If the source path is a folder all its contents will be copied.

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

CURL Command

curl -X POST https://api.dropboxapi.com/2/files/copy_v2 \
    --header "Authorization: Bearer DROPBOX-ACCESS-TOKEN" \
    --header "Content-Type: application/json" \
    --data "{\"from_path\": \"/ocean/starfish.jpg\",\"to_path\": \"/ocean/starfish2.jpg\",\"allow_shared_folder\": false,\"autorename\": false,\"allow_ownership_transfer\": 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/copy_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('from_path','/ocean/starfish.jpg');
$json->UpdateString('to_path','/ocean/starfish2.jpg');
$json->UpdateBool('allow_shared_folder',false);
$json->UpdateBool('autorename',false);
$json->UpdateBool('allow_ownership_transfer',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/copy_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

$metadataTag = $jsonResponse->stringOf('metadata.\'.tag\'');
$metadataName = $jsonResponse->stringOf('metadata.name');
$metadataPath_lower = $jsonResponse->stringOf('metadata.path_lower');
$metadataPath_display = $jsonResponse->stringOf('metadata.path_display');
$metadataId = $jsonResponse->stringOf('metadata.id');
$metadataClient_modified = $jsonResponse->stringOf('metadata.client_modified');
$metadataServer_modified = $jsonResponse->stringOf('metadata.server_modified');
$metadataRev = $jsonResponse->stringOf('metadata.rev');
$metadataSize = $jsonResponse->IntOf('metadata.size');
$metadataContent_hash = $jsonResponse->stringOf('metadata.content_hash');

?>

Sample JSON Response Body

{
  "metadata": {
    ".tag": "file",
    "name": "starfish2.jpg",
    "path_lower": "/ocean/starfish2.jpg",
    "path_display": "/ocean/starfish2.jpg",
    "id": "id:JSXYsxHo1hAAAAAAAAAADg",
    "client_modified": "2018-10-22T22:38:18Z",
    "server_modified": "2018-10-22T22:40:38Z",
    "rev": "1c482db15f",
    "size": 6229,
    "content_hash": "9fa9d692d0762ee759c22dea993c7de0068f9ba2ca842e761a1636d3b0b3cbba"
  }
}