Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • Search
Menu

Belajar PHP: Upload dokumen dan file ke Dropbox

Posted on July 14, 2013

Dropbox adalah solusi penyimpanan dokumen dan file online lintas perangkat dengan satu akun, sehingga pemilik akun dapat mengakses file-file diberbagai perangkat PC, notebook, netbook, iPad, mobile dll. Dropbox sendiri sudah menyediakan aplikasi-aplikasi bawaan khusus untuk perangkat yang akan digunakan.

Bagaimana jika kita ingin mencadangkan sebuah file/dokumen dalam server kita ke akun dropbox dengan aplikasi berbasis PHP? Caranya mudah, Dropbox telah menyediakan PHP DropboxUploader.

Untuk menggunakannya, contoh:

[sourcecode language=”php”]
require ‘DropboxUploader.php’;

$uploader = new DropboxUploader(’email.address.used.to.log.in.to.dropbox@example.com’, ‘dropbox-password’);
$uploader->upload(‘file-to-be-uploaded’, ‘destination-folder-in-dropbox’);
[/sourcecode]

Sedangkan berkas Dropbox Uploader sendiri sebagai berikut:
[sourcecode language=”php”]

<?php
/**
* Dropbox Uploader
*
* Copyright (c) 2009 Jaka Jancar
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Jaka Jancar [jaka@kubje.org] [http://jaka.kubje.org/]
* @version 1.1.5
*/
class DropboxUploader {
protected $email;
protected $password;
protected $caCertSourceType = self::CACERT_SOURCE_SYSTEM;
const CACERT_SOURCE_SYSTEM = 0;
const CACERT_SOURCE_FILE = 1;
const CACERT_SOURCE_DIR = 2;
protected $caCertSource;
protected $loggedIn = false;
protected $cookies = array();

/**
* Constructor
*
* @param string $email
* @param string|null $password
*/
public function __construct($email, $password) {
// Check requirements
if (!extension_loaded(‘curl’))
throw new Exception(‘DropboxUploader requires the cURL extension.’);

$this->email = $email;
$this->password = $password;
}

public function setCaCertificateFile($file)
{
$this->caCertSourceType = self::CACERT_SOURCE_FILE;
$this->caCertSource = $file;
}

public function setCaCertificateDir($dir)
{
$this->caCertSourceType = self::CACERT_SOURCE_DIR;
$this->caCertSource = $dir;
}

public function upload($filename, $remoteDir=’/’) {
if (!file_exists($filename) or !is_file($filename) or !is_readable($filename))
throw new Exception("File ‘$filename’ does not exist or is not readable.");

if (!is_string($remoteDir))
throw new Exception("Remote directory must be a string, is ".gettype($remoteDir)." instead.");

if (!$this->loggedIn)
$this->login();

$data = $this->request(‘https://www.dropbox.com/home’);
$token = $this->extractToken($data, ‘https://dl-web.dropbox.com/upload’);

$data = $this->request(‘https://dl-web.dropbox.com/upload’, true, array(‘plain’=>’yes’, ‘file’=>’@’.$filename, ‘dest’=>$remoteDir, ‘t’=>$token));
if (strpos($data, ‘HTTP/1.1 302 FOUND’) === false)
throw new Exception(‘Upload failed!’);
}

protected function login() {
$data = $this->request(‘https://www.dropbox.com/login’);
$token = $this->extractToken($data, ‘/login’);

$data = $this->request(‘https://www.dropbox.com/login’, true, array(‘login_email’=>$this->email, ‘login_password’=>$this->password, ‘t’=>$token));

if (stripos($data, ‘location: /home’) === false)
throw new Exception(‘Login unsuccessful.’);

$this->loggedIn = true;
}

protected function request($url, $post=false, $postData=array()) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
switch ($this->caCertSourceType) {
case self::CACERT_SOURCE_FILE:
curl_setopt($ch, CURLOPT_CAINFO, $this->caCertSource);
break;
case self::CACERT_SOURCE_DIR:
curl_setopt($ch, CURLOPT_CAPATH, $this->caCertSource);
break;
}
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($post) {
curl_setopt($ch, CURLOPT_POST, $post);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}

// Send cookies
$rawCookies = array();
foreach ($this->cookies as $k=>$v)
$rawCookies[] = "$k=$v";
$rawCookies = implode(‘;’, $rawCookies);
curl_setopt($ch, CURLOPT_COOKIE, $rawCookies);

$data = curl_exec($ch);

if ($data === false)
throw new Exception(‘Cannot execute request: ‘.curl_error($ch));

// Store received cookies
preg_match_all(‘/Set-Cookie: ([^=]+)=(.*?);/i’, $data, $matches, PREG_SET_ORDER);
foreach ($matches as $match)
$this->cookies[$match[1]] = $match[2];

curl_close($ch);

return $data;
}

protected function extractToken($html, $formAction) {
if (!preg_match(‘/<form [^>]*’.preg_quote($formAction, ‘/’).'[^>]*>.*?(<input [^>]*name="t" [^>]*value="(.*?)"[^>]*>).*?<\/form>/is’, $html, $matches) || !isset($matches[2]))
throw new Exception("Cannot extract token! (form action=$formAction)");
return $matches[2];
}

}

[/sourcecode]

atau Unduh dari file berikut DropboxUploader.php (klik kanan save-as)

Terbaru

  • Caranya Ngebangun Website Directory dengan Traffic Tinggi dalam Seminggu!
  • Cara Mengembangkan Channel YouTube Shorts Tanpa Wajah
  • Inilah Cara Menghitung Diskon Baju Lebaran Biar Nggak Bingung Saat Belanja di Mall!
  • Cara Jitu Ngebangun Bisnis SaaS di Era AI Pakai Strategi Agentic Workflow
  • Inilah Rincian Gaji Polri Lulusan Baru 2026, Cek Perbedaan Jalur Akpol, Bintara, dan Tamtama Sebelum Daftar!
  • Inilah 5 Channel YouTube Membosankan yang Diam-diam Menghasilkan Banyak Uang
  • Inilah Cara Pakai Google Maps Offline Biar Mudik Lebaran 2026 Nggak Nyasar Meski Tanpa Sinyal!
  • Inilah Alasan Mahkamah Agung Tolak Kasasi Google, Denda Rp202,5 Miliar Resmi Menanti Akibat Praktik Monopoli
  • Inilah Cara Daftar dan Syarat SPMB SMK Boarding Jawa Tengah 2026, Sekolah Gratis Sampai Lulus!
  • Inilah Daftar Sekolah Kedinasan 2026 untuk Lulusan SMK, Bisa Kuliah Gratis dan Berpeluang Besar Langsung Jadi CPNS!
  • Inilah Pajak TER: Skema Baru PPh 21 yang Nggak Bikin Pusing, Begini Cara Hitungnya!
  • Inilah Jadwal Resmi Jam Buka Tol Jogja-Solo Segmen Prambanan-Purwomartani Saat Mudik Lebaran 2026
  • Inilah Cara Mendapatkan Witherbloom di Fisch Roblox, Rahasia Menangkap Ikan Paling Sulit di Toxic Grove!
  • Kenapa Indomart Point Bisa Kalahkan Bisnis Kafe?
  • Inilah Cara Mendapatkan Rotten Seed di Fisch Roblox, Lokasi Rahasia di Toxic Grove Buat Unlock Toxic Lotus!
  • Inilah Cara Zakat Crypto Kalian Bisa Jadi Pengurang Pajak Berdasarkan Aturan Resmi Pemerintah!
  • Inilah Perbandingan Airwallex vs Payoneer 2026: Jangan Sampai Profit Kalian Ludes Gara-Gara Biaya Admin!
  • Inilah Roadmap 7 Tahap Bangun Bisnis Digital dari Nol Biar Nggak Cuma Putar-Putar di Tempat!
  • Inilah Cara Tetap Gajian dari YouTube Meski View Masih Ratusan, Penasaran?
  • Inilah Alasan Akun TikTok Affiliate GMV 270 Juta Kena Banned Permanen!
  • Inilah Bahaya Astute Beta Server APK, Jangan Sembarang Klik Link Download FF Kipas 2026!
  • Inilah Bahaya Nonton Film di LK21 dan IndoXXI, Awas Data Pribadi dan Saldo Rekening Kalian Bisa Ludes!
  • Inilah Kronologi & Video Lengkap Kasus Sejoli Tambelangan Sampang Viral, Ternyata Gini Awal Mulanya!
  • Inilah Alasan Kenapa Koin Nego Neko Shopee Nggak Bisa Dipakai Bayar Full dan Cara Rahasia Dapetinnya!
  • Inilah Cara Menjawab Pertanyaan Apakah di Sekolahmu Sudah Ada IFP/PID dengan Benar dan Profesional
  • Inilah Fakta Isu Roblox Diblokir di Indonesia 2026, Benarkah Akan Ditutup Total?
  • Inilah Penyebab dan Cara Mengatasi FF Kipas My ID Verify UID Biar Akun Tetap Aman
  • Inilah Deretan HP RAM 8GB Harga di Bawah 2 Juta Terbaik 2026, Spek Dewa Tapi Nggak Bikin Kantong Jebol!
  • Contoh Cara Buat SK Panitia TKA 2026
  • Inilah Cara Download Point Blank ID Versi Terbaru 2026, Gampang Banget Ternyata!
  • How to Fix Python Not Working in VS Code Terminal: A Troubleshooting Guide
  • Game File Verification Stuck at 0% or 99%: What is it and How to Fix the Progress Bar?
  • Why Does PowerPoint Underline Hyperlinks? Here is How to Remove Them
  • AI Bug Hunting with Semgrep
  • What is the Excel Power Query 0xc000026f Error?
  • Claude Code Tips: Don’t Overuse SKILL.md!
  • How to Planning Cinematic AI Film Production: A Step-by-Step Tutorial Using LitMedia Tools
  • 6 Innovative AI Tools for 2026: From Voice Cloning to Advanced Automation Systems
  • How to Run Hunter Alpha: The Free 1 Trillion Parameter AI Agent on OpenClaw
  • Build Your Own Self-Improving AI: A Guide to Andrej Karpathy’s Autoresearch and Claude Code
  • Apa itu Spear-Phishing via npm? Ini Pengertian dan Cara Kerjanya yang Makin Licin
  • Apa Itu Predator Spyware? Ini Pengertian dan Kontroversi Penghapusan Sanksinya
  • Mengenal Apa itu TONESHELL: Backdoor Berbahaya dari Kelompok Mustang Panda
  • Siapa itu Kelompok Hacker Silver Fox?
  • Apa itu CVE-2025-52691 SmarterMail? Celah Keamanan Paling Berbahaya Tahun 2025

©2026 emka.web.id | Design: Newspaperly WordPress Theme