PHP code example of yangweijie / filesystem-ctfile

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

    

yangweijie / filesystem-ctfile example snippets



// test-installation.php
le\CtFileAdapter;
use YangWeijie\FilesystemCtfile\CtFileClient;
use YangWeijie\FilesystemCtfile\ConfigurationManager;

echo "Testing Filesystem ctFile Extension Installation..." . PHP_EOL;

// Test 1: Check if main classes are available
$classes = [
    CtFileAdapter::class,
    CtFileClient::class,
    ConfigurationManager::class,
];

foreach ($classes as $class) {
    if (class_exists($class)) {
        echo "✓ {$class} - OK" . PHP_EOL;
    } else {
        echo "✗ {$class} - MISSING" . PHP_EOL;
        exit(1);
    }
}

// Test 2: Test configuration validation
try {
    $config = new ConfigurationManager([
        'ctfile' => [
            'host' => 'test.example.com',
            'username' => 'test',
            'password' => 'test',
        ]
    ]);
    echo "✓ Configuration validation - OK" . PHP_EOL;
} catch (Exception $e) {
    echo "✗ Configuration validation - FAILED: " . $e->getMessage() . PHP_EOL;
    exit(1);
}

echo PHP_EOL . "Installation verification completed successfully!" . PHP_EOL;
echo "You can now use the filesystem-ctfile package in your project." . PHP_EOL;



use League\Flysystem\Filesystem;
use YangWeijie\FilesystemCtfile\CtFileAdapter;

// Configure the ctFile adapter
$config = [
    'ctfile' => [
        'host' => 'your-ctfile-host.com',
        'port' => 21,
        'username' => 'your-username',
        'password' => 'your-password',
        'ssl' => false,
        'passive' => true,
    ],
];

// Create the adapter and filesystem
$adapter = new CtFileAdapter($config);
$filesystem = new Filesystem($adapter);

// Use standard Flysystem operations
$filesystem->write('path/to/file.txt', 'Hello, World!');
$content = $filesystem->read('path/to/file.txt');
$exists = $filesystem->fileExists('path/to/file.txt');
$filesystem->delete('path/to/file.txt');

$config = [
    'ctfile' => [
        'host' => 'your-ctfile-host.com',      // Required: ctFile server host
        'port' => 21,                          // Optional: Server port (default: 21)
        'username' => 'your-username',         // Required: Authentication username
        'password' => 'your-password',         // Required: Authentication password
        'timeout' => 30,                       // Optional: Connection timeout (default: 30)
        'ssl' => false,                        // Optional: Use SSL connection (default: false)
        'passive' => true,                     // Optional: Use passive mode (default: true)
    ],
    'adapter' => [
        'root_path' => '/',                    // Optional: Root path prefix (default: /)
        'path_separator' => '/',               // Optional: Path separator (default: /)
        'case_sensitive' => true,              // Optional: Case sensitive paths (default: true)
        'create_directories' => true,          // Optional: Auto-create directories (default: true)
    ],
    'logging' => [
        'enabled' => false,                    // Optional: Enable logging (default: false)
        'level' => 'info',                     // Optional: Log level (default: info)
        'channel' => 'filesystem-ctfile',      // Optional: Log channel (default: filesystem-ctfile)
    ],
    'cache' => [
        'enabled' => false,                    // Optional: Enable caching (default: false)
        'ttl' => 300,                          // Optional: Cache TTL in seconds (default: 300)
        'driver' => 'memory',                  // Optional: Cache driver (default: memory)
    ],
];

// Create directories
$filesystem->createDirectory('new/directory');

// List directory contents
$listing = $filesystem->listContents('path/to/directory');
foreach ($listing as $item) {
    echo $item->path() . ' - ' . $item->type() . PHP_EOL;
}

// Check if directory exists
if ($filesystem->directoryExists('path/to/directory')) {
    // Directory exists
}

// Delete directory
$filesystem->deleteDirectory('path/to/directory');

// Write files
$filesystem->write('file.txt', 'content');
$filesystem->writeStream('large-file.txt', $stream);

// Read files
$content = $filesystem->read('file.txt');
$stream = $filesystem->readStream('large-file.txt');

// File metadata
$size = $filesystem->fileSize('file.txt');
$mimeType = $filesystem->mimeType('file.txt');
$lastModified = $filesystem->lastModified('file.txt');

// Copy and move files
$filesystem->copy('source.txt', 'destination.txt');
$filesystem->move('old-name.txt', 'new-name.txt');

use League\Flysystem\UnableToWriteFile;
use YangWeijie\FilesystemCtfile\Exceptions\CtFileException;

try {
    $filesystem->write('protected/file.txt', 'content');
} catch (UnableToWriteFile $e) {
    // Handle Flysystem exceptions
    echo "Failed to write file: " . $e->getMessage();
} catch (CtFileException $e) {
    // Handle ctFile-specific exceptions
    echo "ctFile error: " . $e->getMessage();
}

use YangWeijie\FilesystemCtfile\CtFileClient;
use YangWeijie\FilesystemCtfile\CtFileAdapter;
use YangWeijie\FilesystemCtfile\ConfigurationManager;

// Advanced configuration with custom client
$config = new ConfigurationManager([
    'ctfile' => [
        'host' => 'ctfile.example.com',
        'username' => 'user',
        'password' => 'pass',
        'timeout' => 60,
    ],
    'adapter' => [
        'root_path' => '/app/files',
        'create_directories' => true,
    ],
]);

$client = new CtFileClient($config->get('ctfile'));
$adapter = new CtFileAdapter($client, $config->get('adapter'));

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// Create a logger
$logger = new Logger('ctfile');
$logger->pushHandler(new StreamHandler('ctfile.log', Logger::INFO));

// Configure adapter with logging
$config['logging'] = [
    'enabled' => true,
    'level' => 'info',
];

$adapter = new CtFileAdapter($config);
$adapter->setLogger($logger);

   // Increase timeout in configuration
   $config['ctfile']['timeout'] = 120;
   

   // Check credentials and permissions
   $config['ctfile']['username'] = 'correct-username';
   $config['ctfile']['password'] = 'correct-password';
   

   // Ensure directories exist or enable auto-creation
   $config['adapter']['create_directories'] = true;
   

// Enable debug logging
$config['logging'] = [
    'enabled' => true,
    'level' => 'debug',
];
bash
# Check PHP version (must be 8.1+)
php --version

# Check Composer version (must be 2.0+)
composer --version

# Verify d('json') ? 'OK' : 'MISSING') . PHP_EOL;
echo 'MBString: ' . (extension_loaded('mbstring') ? 'OK' : 'MISSING') . PHP_EOL;
echo 'OpenSSL: ' . (extension_loaded('openssl') ? 'OK' : 'MISSING') . PHP_EOL;
"
bash
php test-installation.php
bash
rm test-installation.php
bash
   # Check available PHP versions
   php --version
   
   # If using multiple PHP versions, specify the correct one
   /usr/bin/php8.1 -v
   composer config platform.php 8.1.0