Application
Quite often, when developing web applications, you have to think about the organization of a stable storage of user files. And if at the same time we are limited by disk space, then the creation of file sharing of moderate severity, and even with the backup of them, greatly puzzles us.
Decision
As a solution, I suggest the
PHP <-> Dropbox scheme. Dropbox is interesting to us because 2Gb of virtual disk space is allocated to each new user, as well as the ability to roll back to previous versions of files. This provides us with a stable repository, in which there will be no cases of permanently deleted files.
Customization
Training
First we need to add the
Oauth library to PHP:
sudo pecl install oauth
')
If an error occurs during compilation, then put the developer version of
PECL :
sudo apt-get install libpcre3-dev
Then add to php.ini:
extension=oauth.so
Installation
For further work, we need to
download the library to work with Dropbox in PHP. After downloading, unpack the
Dropbox folder from the archive into the scripts directory of our application, place the files from the
examples folder nearby. With these examples, I will show you how to:
- create a new dropbox account
- upload files to dropbox account
- receive these files
- delete files
Job
Immediately I will warn you that we need an account on Dropbox to work, through which we will register our new application and get the keys for it. You can register on the
Dropbox site. After registration, visit
the developer section and add your new application. You will receive
App keys (Key and Secret) that will allow our application to work with Dropbox.
Create user
Now open the
createaccount.php file that you unpacked next to the Dropbox folder. Set values for the
consumerKey and
consumerSecret variables:
$consumerKey = '';
Now set the necessary parameters of the createAccount () function:
var_dump($dropbox->createAccount('mrhandsome@example.org','Mr','Handsome','password goes here'));
Run this script and, thereby, you will create a new account on Dropbox (for example, for a new user of your site).
Upload a file to Dropbox
Now, to upload the file to the repository of the created user, you need to create a new script:
<?php $consumerKey = '';
Downloading a file from Dropbox
To download a file, create a script with the same content, but instead of the putFile () function, we use getFile ();
echo( $dropbox->getFile('newPath.txt') );
Delete files on Dropbox
To delete by analogy, use delete ();
$dropbox->delete('newPath.txt');
Account statistics
To monitor free space on Dropbox, use the following:
var_dump($dropbox->getAccountInfo());
This method provides statistics on the user and the occupied disk space. I think that 2Gb for the user's personal files is quite enough, but if he still fills it, you can warn the user that he will soon have to delete something.
Conclusion
In conclusion, I want to add that I have listed only the most basic functions. I recommend to familiarize yourself with
the Dropbox API for PHP in order to use the maximum integration capabilities.