PHP code example of sahablibya / laravel-sharepoint-filesystem

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

    

sahablibya / laravel-sharepoint-filesystem example snippets


'disks' => [
    // ... other disks

    'sharepoint' => [
        'driver' => 'sharepoint',
        'client_id' => env('GRAPH_CLIENT_ID'),
        'client_secret' => env('GRAPH_CLIENT_SECRET'),
        'tenant_id' => env('GRAPH_TENANT_ID', 'common'),
        'drive_id' => env('SHAREPOINT_DRIVE_ID'),
        'root_item_id' => env('SHAREPOINT_ROOT_ITEM_ID'), // Optional Parent Folder Item ID
        'prefix' => env('SHAREPOINT_PREFIX', ''), // Optional
        'copy_monitor_timeout' => env('SHAREPOINT_COPY_MONITOR_TIMEOUT', 300),
        'copy_monitor_interval_ms' => env('SHAREPOINT_COPY_MONITOR_INTERVAL_MS', 1000),
        'throw' => true, // Surface failures during setup
    ],
],

'onedrive' => [
    'driver' => 'onedrive',
    'auth_mode' => 'device_code',
    'client_id' => env('ONEDRIVE_CLIENT_ID'),
    'tenant_id' => 'consumers', // Use "common" to also allow work accounts
    'token_key' => 'personal-backups',
    'root_item_id' => env('ONEDRIVE_ROOT_ITEM_ID'),
    'prefix' => env('ONEDRIVE_PREFIX', 'backups'),
    'throw' => true,
],

'sharepoint' => [
    'driver' => 'sharepoint',
    'auth_mode' => 'client_credentials',
    'client_id' => env('GRAPH_CLIENT_ID'),
    'client_secret' => env('GRAPH_CLIENT_SECRET'),
    'tenant_id' => env('GRAPH_TENANT_ID'),
    'drive_id' => env('SHAREPOINT_DRIVE_ID'),
    'root_item_id' => env('SHAREPOINT_ROOT_ITEM_ID'),
    'prefix' => env('SHAREPOINT_PREFIX', ''),
    'throw' => true,
],

Storage::disk('sharepoint')->put(
    'fssi-backup/example.zip',
    'backup contents',
);

use Illuminate\Support\Facades\Storage;

// Write a file
Storage::disk('sharepoint')->put('documents/report.pdf', $contents);

// Write from a stream (memory efficient for large files)
$stream = fopen('/path/to/large-file.zip', 'r');
Storage::disk('sharepoint')->writeStream('backups/large-file.zip', $stream);

// Read a file
$contents = Storage::disk('sharepoint')->get('documents/report.pdf');

// Read as stream
$stream = Storage::disk('sharepoint')->readStream('documents/report.pdf');

// Check if file exists
if (Storage::disk('sharepoint')->exists('documents/report.pdf')) {
    // File exists
}

// Delete a file
Storage::disk('sharepoint')->delete('documents/report.pdf');

// Delete multiple files
Storage::disk('sharepoint')->delete(['file1.pdf', 'file2.pdf']);

// Copy a file
Storage::disk('sharepoint')->copy('old.pdf', 'new.pdf');

// Move a file
Storage::disk('sharepoint')->move('old-location.pdf', 'new-location.pdf');

// Create a directory
Storage::disk('sharepoint')->makeDirectory('documents/2024');

// List files in a directory
$files = Storage::disk('sharepoint')->files('documents');

// List all files recursively
$files = Storage::disk('sharepoint')->allFiles('documents');

// List directories
$directories = Storage::disk('sharepoint')->directories('documents');

// List all directories recursively
$directories = Storage::disk('sharepoint')->allDirectories('documents');

// Delete a directory
Storage::disk('sharepoint')->deleteDirectory('old-documents');

// Get file size
$size = Storage::disk('sharepoint')->size('documents/report.pdf');

// Get last modified time
$timestamp = Storage::disk('sharepoint')->lastModified('documents/report.pdf');

// Get MIME type
$mimeType = Storage::disk('sharepoint')->mimeType('documents/report.pdf');

// Store an uploaded file
$path = $request->file('document')->store('uploads', 'sharepoint');

// Download a file
return Storage::disk('sharepoint')->download('documents/report.pdf');

// Download with custom name
return Storage::disk('sharepoint')->download('documents/report.pdf', 'custom-name.pdf');

return [
    'backup' => [
        'name' => env('BACKUP_NAME', 'imamMalik_schools_system_backup'),
        // Keep your published source and other backup settings here.
        'destination' => [
            'filename_prefix' => env('BACKUP_FILENAME_PREFIX', 'imamMalik_schools_system_backup_'),
            'disks' => ['onedrive'],
            // Keep your published compression and other destination settings.
        ],
    ],
    'monitor_backups' => [
        [
            'name' => env('BACKUP_NAME', 'imamMalik_schools_system_backup'),
            'disks' => ['onedrive'],
            'health_checks' => [
                \Spatie\Backup\Tasks\Monitor\HealthChecks\MaximumAgeInDays::class => 1,
                \Spatie\Backup\Tasks\Monitor\HealthChecks\MaximumStorageInMegabytes::class => 5000,
            ],
        ],
    ],
    // Keep your published notifications and cleanup settings.
];

'disks' => [
    'sharepoint-hr' => [
        'driver' => 'sharepoint',
        'client_id' => env('GRAPH_CLIENT_ID'),
        'client_secret' => env('GRAPH_CLIENT_SECRET'),
        'tenant_id' => env('GRAPH_TENANT_ID'),
        'drive_id' => 'hr-drive-id',
        'prefix' => 'employee-files',
    ],
    
    'sharepoint-finance' => [
        'driver' => 'sharepoint',
        'client_id' => env('GRAPH_CLIENT_ID'),
        'client_secret' => env('GRAPH_CLIENT_SECRET'),
        'tenant_id' => env('GRAPH_TENANT_ID'),
        'drive_id' => 'finance-drive-id',
        'prefix' => 'reports',
    ],
],

'onedrive' => [
    'driver' => 'onedrive',
    'auth_mode' => 'client_credentials',
    'client_id' => env('GRAPH_CLIENT_ID'),
    'client_secret' => env('GRAPH_CLIENT_SECRET'),
    'tenant_id' => env('GRAPH_TENANT_ID'),
    'drive_id' => env('ONEDRIVE_DRIVE_ID'),
    'root_item_id' => env('ONEDRIVE_ROOT_ITEM_ID'),
    'prefix' => env('ONEDRIVE_PREFIX', ''),
],

'copy_monitor_timeout' => 300, // seconds
'copy_monitor_interval_ms' => 1000, // milliseconds

(function (): void {
    $diskName = 'onedrive'; // Change to your configured disk name.
    $diskConfig = config("filesystems.disks.{$diskName}", []);

    dump([
        'project' => base_path(),
        'environment' => app()->environment(),
        'configuration_cached' => app()->configurationIsCached(),
        'disk_name' => $diskName,
        'disk' => \Illuminate\Support\Arr::only($diskConfig, [
            'driver', 'auth_mode', 'drive_id', 'root_item_id', 'prefix', 'throw',
        ]),
        'backup_name' => config('backup.backup.name'),
        'destination_disks' => config('backup.backup.destination.disks'),
        'filename_prefix' => config('backup.backup.destination.filename_prefix'),
        'monitor_backups' => array_map(
            static fn (array $monitor): array => \Illuminate\Support\Arr::only($monitor, ['name', 'disks']),
            config('backup.monitor_backups', []),
        ),
    ]);
})();

(function (): void {
    $diskName = 'onedrive'; // Or 'sharepoint'.
    $diskConfig = config("filesystems.disks.{$diskName}");
    if (! is_array($diskConfig)) {
        throw new \RuntimeException('The selected disk is not configured.');
    }
    $disk = \Illuminate\Support\Facades\Storage::build(
        array_replace($diskConfig, ['throw' => true]),
    );
    $path = 'sharepoint-check-'.bin2hex(random_bytes(16)).'.txt';
    $expected = "SharePoint filesystem console verification.\n";

    if ($disk->exists($path)) {
        throw new \RuntimeException('Temporary name already exists; stop and generate a new name.');
    }

    echo "Temporary disk-relative path: {$path}\n";
    $writeAttempted = false;
    try {
        echo "Uploading harmless content...\n";
        $writeAttempted = true;
        if ($disk->put($path, $expected) !== true) {
            throw new \RuntimeException('Temporary upload failed.');
        }

        echo "Reading and comparing content...\n";
        if ($disk->get($path) !== $expected) {
            throw new \RuntimeException('Temporary file content did not match.');
        }
        echo "Upload and read-back verification passed.\n";
    } finally {
        if ($writeAttempted) {
            try {
                if ($disk->delete($path) !== true) {
                    throw new \RuntimeException('Temporary file deletion failed.');
                }
                echo "Temporary file deleted.\n";
            } catch (\Throwable) {
                // Preserve the original failure, if any, without dumping HTTP details.
                echo "Cleanup not confirmed; inspect only this temporary path: {$path}\n";
            }
        }
    }
})();
bash
php artisan onedrive:connect