使用curl PHP将.tar.gz或.sql文件从服务器上传到Dropbox

时间:2018-10-24 06:35:51

标签: php curl

我想将我的每日备份上传到保管箱帐户。我为此使用curl php并使用以下代码制作一个文件...

<?php
    $api_url = 'https://www.dropbox.com/home/DROP_BOX_FOLDER_NAME'; 
    $token = 'TOKEN'; 

    $headers = array('Authorization: Bearer '. $token,
        'Content-Type: application/octet-stream',
        'Dropbox-API-Arg: '.
        json_encode(
            array(
                "path"=> '23_10_2018--20_35_01.tar.gz',
                "mode" => "add",
                "autorename" => true,
                "mute" => false
            )
        )
    );
    $ch = curl_init($api_url);

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POST, true);

    $path = $filename;
    $fp = fopen($path, 'rb');
    $filesize = filesize($path);

    curl_setopt($ch, CURLOPT_POSTFIELDS, fread($fp, $filesize));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($ch, CURLOPT_VERBOSE, 1); // debug

    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    echo($response.'<br/>');
    echo($http_code.'<br/>');

    curl_close($ch);
?>

具有上述代码的文件位于我的服务器中,而我的传输文件(.sql或.taz.gz)也位于该服务器中。 当我运行该文件时,它不会产生任何错误,但是不会将我的文件从服务器传输到Dropbox。 我对$ api_url感兴趣。

1 个答案:

答案 0 :(得分:0)

尝试

<?php
ini_set('display_errors',1);
$api_url = 'https://content.dropboxapi.com/2/files/upload'; 
//$api_url = 'https://www.dropbox.com/home/DROP_BOX_FOLDER_NAME'; 

$filename ='samples.tar.gz';

$token = 'MY TOKEN'; 

$headers = array('Authorization: Bearer '. $token,
    'Content-Type: application/octet-stream',
    'Dropbox-API-Arg: '.
    json_encode(
        [
            "path"=> '/'.$filename,
            "mode" => "add",
            "autorename" => true,
            "mute" => false,
            "strict_conflict"=>false
        ]
    ),
    'data-binary @'.$filename
);
$ch = curl_init($api_url);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);

$path = $filename;
$fp = fopen($path, 'rb');
$filesize = filesize($path);

curl_setopt($ch, CURLOPT_POSTFIELDS, fread($fp, $filesize));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_VERBOSE, 1); // debug

$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo($response.'<br/>');
echo($http_code.'<br/>');

curl_close($ch);