PHP code example of kalprajsolutions / laravel-onedrive-filesystem

1. Go to this page and download the library: Download kalprajsolutions/laravel-onedrive-filesystem 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/ */

    

kalprajsolutions / laravel-onedrive-filesystem example snippets


'disks' => [
    // ...

    'onedrive' => [
        'driver'       => 'onedrive',
        'client_id'    => env('GRAPH_CLIENT_ID'),
        'tenant_id'    => env('GRAPH_TENANT_ID'),
        'client_secret'=> env('GRAPH_CLIENT_SECRET'),
        'user_id'      => env('GRAPH_USER_ID'),
        'base_path'    => env('GRAPH_BASE_PATH'),
    ],
],

'disks' => [
    'onedrive' => [
        'driver'       => 'onedrive',
        'client_id'    => env('GRAPH_CLIENT_ID'),
        'tenant_id'    => env('GRAPH_TENANT_ID'),
        'client_secret'=> env('GRAPH_CLIENT_SECRET'),
        'user_id'      => env('GRAPH_USER_ID'),
        'base_path'    => env('GRAPH_BASE_PATH'),
    ],
    'onedrive_backup' => [
        'driver'       => 'onedrive',
        'client_id'    => env('BACKUP_GRAPH_CLIENT_ID'),
        'tenant_id'    => env('BACKUP_GRAPH_TENANT_ID'),
        'client_secret'=> env('BACKUP_GRAPH_CLIENT_SECRET'),
        'user_id'      => env('BACKUP_GRAPH_USER_ID'),
        'base_path'    => env('BACKUP_GRAPH_BASE_PATH'),
    ],
],

use Illuminate\Support\Facades\Storage;

// Write a file
Storage::disk('onedrive')->put('documents/report.txt', 'Report content');

// Read a file
$content = Storage::disk('onedrive')->get('documents/report.txt');

// Upload with auto-generated filename
Storage::disk('onedrive')->putFile('avatars', $request->file('avatar'));

// Upload with a custom filename
Storage::disk('onedrive')->putFileAs('documents', $request->file('document'), 'custom-name.pdf');

// Return a download response
return Storage::disk('onedrive')->download('document.pdf', 'my-document.pdf');

// Get a sharing URL
$url = Storage::disk('onedrive')->getUrl('document.pdf');

// Stream large files
$source = Storage::disk('onedrive')->readStream('large-file.zip');
$dest   = fopen(storage_path('large-file.zip'), 'w');

stream_copy_to_stream($source, $dest);

fclose($source);
fclose($dest);

// Check existence
$exists = Storage::disk('onedrive')->exists('filename.txt');

// Copy
Storage::disk('onedrive')->copy('source.txt', 'destination.txt');

// Move / Rename
Storage::disk('onedrive')->move('old-name.txt', 'new-name.txt');

// Delete
Storage::disk('onedrive')->delete('filename.txt');

$size      = Storage::disk('onedrive')->size('filename.txt');
$timestamp = Storage::disk('onedrive')->lastModified('filename.txt');
$mime      = Storage::disk('onedrive')->mimeType('filename.txt');

// Create a directory
Storage::disk('onedrive')->createDirectory('Documents/NewFolder');

// List files
$files = Storage::disk('onedrive')->files('Documents');

// List files recursively
$allFiles = Storage::disk('onedrive')->allFiles('Documents');

// List directories
$directories = Storage::disk('onedrive')->directories('Documents');

// Delete a directory and its contents
Storage::disk('onedrive')->deleteDirectory('Documents/OldFolder');

// config/backup.php
'destination' => [
    'disks' => ['onedrive'],
],

/**
 * Microsoft Graph's simple "PUT .../content" endpoint only supports
 * files up to 4 MiB. Anything larger MUST go through an upload session.
 *
 * @see https://learn.microsoft.com/en-us/graph/api/driveitem-put-content
 */
private const SIMPLE_UPLOAD_MAX_BYTES = 4 * 1024 * 1024;

/**
 * Each upload-session fragment (except the last) must be a multiple of
 * 320 KiB, and the docs warn you may be throttled above 60 MiB per
 * request. 10 MiB is a safe multiple of 320 KiB well under that ceiling.
 *
 * @see https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession
 */
private const CHUNK_SIZE = 10 * 1024 * 1024;
bash
php artisan vendor:publish --tag=onedrive-config
bash
php artisan backup:run