PHP code example of rymanalu / laravel-simple-uploader
1. Go to this page and download the library: Download rymanalu/laravel-simple-uploader library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rymanalu / laravel-simple-uploader example snippets
Uploader::from('request')->upload('avatar'); // see the supported providers at config/uploader.php
// Or you can use the magic methods...
Uploader::fromRequest()->upload('file');
Uploader::fromLocal()->upload('/path/to/file');
Uploader::fromUrl()->upload('https://via.placeholder.com/150.png');
// If your default provider is local, it will automatically use the local provider.
Uploader::upload('/path/to/file');
namespace App\Http\Controllers;
use Uploader;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Change user's avatar.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function changeAvatar(Request $request)
{
Uploader::upload('avatar');
//
}
}
// The parameter in the Closure is a full uploaded filename...
Uploader::upload('avatar', function ($filename) {
Photo::create(['photo' => $filename]);
});
Uploader::upload('/path/to/file', function ($filename) {
$user = User::find(12);
$user->update(['avatar' => $filename]);
});
// see the supported uploadTo parameter at config/filesystems.php
Uploader::uploadTo('s3')->upload('avatar');
// Or you can use the magic methods...
Uploader::uploadToS3();
Uploader::uploadToFtp();
Uploader::uploadToLocal();
Uploader::uploadToRackspace();
// You are free to place the providers anywhere you like...
namespace App\Uploader\Providers;
// Check this interface to see all the docblock for each method...
use Rymanalu\LaravelSimpleUploader\Contracts\Provider;
class GoogleDrive implements Provider
{
public function isValid() {}
public function getContents() {}
public function getExtension() {}
public function setFile($file) {} // Or you can use Rymanalu\LaravelSimpleUploader\Support\FileSetter trait to implement this method...
}
namespace App\Providers;
use App\Uploader\Providers\GoogleDrive;
use Illuminate\Support\ServiceProvider;
use Rymanalu\LaravelSimpleUploader\Support\Uploader; // Or just "use Uploader;" if you register the facade in the aliases array in "config/app.php" before...
class UploaderServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
Uploader::extend('gdrive', function ($app) {
// Return implementation of Rymanalu\LaravelSimpleUploader\Contracts\Provider...
return new GoogleDrive;
});
}
/**
* Register bindings in the container.
*
* @return void
*/
public function register()
{
//
}
}