PHP code example of diegobvasco / php-rsync

1. Go to this page and download the library: Download diegobvasco/php-rsync 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/ */

    

diegobvasco / php-rsync example snippets


use DiegoVasconcelos\Rsync\Rsync;

$result = (new Rsync())
    ->copy('/path/to/source', '/path/to/destination')
    ->run();

echo $result->summary();
// Copied: 5 files (12.5 KB)
// Deleted: 2 files (1.2 KB)
// Skipped: 3 files

$result = (new Rsync())
    ->copy('/path/to/source', '/path/to/destination')
    ->skip(['*.log', 'vendor/*', '.git', 'node_modules/'])
    ->run();

foreach ($result->copied as $file) {
    echo $file->relativePath . ' - ' . $file->formattedSize() . "\n";
}

$result = (new Rsync())
    ->copy('/path/to/source', '/path/to/destination')
    ->delete()
    ->run();

use DiegoVasconcelos\Rsync\Rsync;
use DiegoVasconcelos\Rsync\Output\TerminalOutput;

// Print each file action to the terminal as it happens
$result = (new Rsync(new TerminalOutput()))
    ->copy('/path/to/source', '/path/to/destination')
    ->delete()
    ->run();

// Output:
// COPY new-file.txt (2.5 KB)
// DELETE old-file.txt
// SKIP unchanged.txt

$result = (new Rsync())
    ->copy('/path/to/source', '/path/to/destination')
    ->delete()
    ->dryRun()
    ->run();

// No files are actually copied or deleted, and the filesystem is never
// mutated (no copies, no deletions, no empty-directory cleanup).
echo $result->summary();

$result->copiedCount();    // Number of files copied
$result->deletedCount();   // Number of files deleted
$result->skippedCount();   // Number of files skipped
$result->totalBytesCopied(); // Total bytes copied
$result->copiedPaths();    // Array of copied file paths
$result->deletedPaths();   // Array of deleted file paths
$result->skippedPaths();   // Array of skipped file paths
$result->summary();        // Human-readable summary string

new Rsync(?Output $output = null, ?Filesystem $filesystem = null)

new TerminalOutput(?resource $stream = null)

$output = new TerminalOutput(fopen('php://memory', 'r+'));

use DiegoVasconcelos\Rsync\Filesystem\InMemoryFilesystem;
use DiegoVasconcelos\Rsync\Rsync;

$fs = new InMemoryFilesystem();
$fs->put('/src/app.php', ' echo 1;');

$result = (new Rsync(null, $fs))->copy('/src', '/dest')->run();
bash
composer