Skip to content

Handling File Uploads

Kunal Varma edited this page Oct 14, 2017 · 3 revisions

Handling File Uploads

File: index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <title>File Upload</title>
</head>
<body>
    <form action="upload.php" method="POST" enctype="multipart/form-data">
        <input type="file" name="file">
        <button type="submit">Upload</button>
    </form>
</body>
</html>

File: upload.php

use Kunnu\Dropbox\Dropbox;
use Kunnu\Dropbox\DropboxApp;
use Kunnu\Dropbox\DropboxFile;
use Kunnu\Dropbox\Exceptions\DropboxClientException;

//Configure Dropbox Application
$app = new DropboxApp("client_id", "client_secret", "access_token");

//Configure Dropbox service
$dropbox = new Dropbox($app);

// Check if file was uploaded
if (isset($_FILES["file"])) {
    // File to Upload
    $file = $_FILES['file'];

    // File Path
    $fileName = $file['name'];
    $filePath = $file['tmp_name'];

    try {
        // Create Dropbox File from Path
        $dropboxFile = new DropboxFile($filePath);

        // Upload the file to Dropbox
        $uploadedFile = $dropbox->upload($dropboxFile, "/" . $fileName, ['autorename' => true]);

        // File Uploaded
        echo $uploadedFile->getPathDisplay();
    } catch (DropboxClientException $e) {
        echo $e->getMessage();
    }
}