Monday, September 5, 2016

How to upload file into Dropbox in Laravel | Working with Dropbox API to upload file | Laravel 5 dropbox api file upload

laravel file upload dropbox





Dropbox a popular file hosting provider provides cloud storage till 2GB for free user. In this tutorial we'll use dropbox api to upload file into dropbox. By using dropbox we can keep backup of out project files, database and so on. So let's start how we can do it ..


1. First we need to install thephpleague/flysystem-dropbox package in our project.
2. Then we need to create a dropbox app and after creating we need to get the App's secret key and token.
3. Let's put this in .env file as following:

DROPBOX_TOKEN=app_token
DROPBOX_SECRET=app_secret

4. Now go to vendor\dropbox\dropbox-sdk\lib\dropbox\RequestUtil.php in your project directory and comment out the following lines :

/*
if (strlen((string) PHP_INT_MAX) < 19) {
    // Looks like we're running on a 32-bit build of PHP.  This could cause problems because some of the numbers
    // we use (file sizes, quota, etc) can be larger than 32-bit ints can handle.
    throw new \Exception("The Dropbox SDK uses 64-bit integers, but it looks like we're running on a version of PHP that doesn't support 64-bit integers (PHP_INT_MAX=" . ((string) PHP_INT_MAX) . ").  Library: \"" . __FILE__ . "\"");
}*/

otherwise later when you run the project you'll get following error:

The Dropbox SDK uses 64-bit integers, but it looks like we're running on a version of PHP that doesn't support 64-bit integers 


5. Now let's create the routes in routes.php
Route::get('dropboxUpload', 'dropController@dropboxUpload');
Route::post('dropboxFileUpload', 'dropController@dropboxFileUpload');

6. Now we gonna make the dropController.php with the following command:

php artisan make:controller dropController 
7. Change the following code with the existing code in dropController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Input;
use Validator;
use Illuminate\Support\Facades\Redirect;
use League\Flysystem\Dropbox\DropboxAdapter;
use League\Flysystem\Filesystem;
use Dropbox\Client;
use Dropbox\WriteMode;

class dropController extends Controller
{
    public function dropboxUpload()
 {
  return view('droboxUpload');
 }
 
    public function dropboxFileUpload()
        {       
         $Client = new Client(env('DROPBOX_TOKEN'), env('DROPBOX_SECRET'));
         $file = fopen(Input::file('image'), 'rb');
         $size = filesize(Input::file('image'));
         $dropboxFileName = '/'.Input::file('image')->getClientOriginalName();
         $Client->uploadFile($dropboxFileName,WriteMode::add(),$file, $size);
         $links['share'] = $Client->createShareableLink($dropboxFileName);
         $links['view'] = $Client->createTemporaryDirectLink($dropboxFileName);
         print_r($links); 
        } 
}

8. Now we gonna create the dropboxUpload.blade.php :

<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Dropbox with Laravel 5</title>
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >
</head>
<body>
 <nav class="navbar navbar-default">
  <div class="container-fluid">
   <div class="navbar-header">
    <a class="navbar-brand" href="#">Upload File to Dropbox using Laravel </a>
   </div>
  </div>
 </nav>
 <div class="container">
  <form style="border: 4px solid #a1a1a1;margin-top: 15px;padding: 10px;" action="{{ URL::to('dropboxFileUpload') }}" class="form-horizontal" method="post" enctype="multipart/form-data" >
   <input type="file" name="image" />
     {!! Form::token(); !!}
       {!!   csrf_field() ; !!} 
   <button class="btn btn-primary">Import File</button>
  </form>

 </div>
</body>
</html>
That's it. When you visit the route you'll see the upload option in view. You upload the file and it'll automatically upload into Dropbox.

Hope this tutorial will help you to upload file into DropBox.














Related Articles

3 comments:

  1. Best Laravel Development Company with expert developers providing laravel web development services. Laravel is best free and open source PHP framework. Contact us : +91-9806724185 or Contact@expresstechsoftwares.com

    ReplyDelete
  2. Wow, amazing block structure! How long
    Have you written a blog before? Working on a blog seems easy.
    The overview of your website is pretty good, not to mention what it does.
    In the content!
    vstpatch.net
    MX Player Pro APK Crack
    Potplayer Crack
    Dropbox Crack
    Maxthon Cloud Browser Crack
    Minitube Crack

    ReplyDelete
  3. which folder we install " thephpleague/flysystem-dropbox package " this package

    ReplyDelete