PHP code example of molajo / filesystem

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

    

molajo / filesystem example snippets



    // Instantiate Handler (example local) and pass it into the Filesystem Adapter

    use Molajo\Filesystem\Handler\Local;
    $handler = new Local();

    use Molajo\Filesystem\Adapter;
    $adapter = new Adapter($handler);

    $true_or_false = $adapter->exists('\file\located\here.txt');

    $metadata = $adapter->getMetadata('\file\located\here.txt');
    echo $metadata->owner;      // See complete list of metadata returned, below

    $contents_of_file = $adapter->read('\file\located\here.txt');

    $list_of_results = $adapter->getList($path, $recursive, $extension_list,
        $


    use Molajo\Filesystem\Handler\Local;
    $handler = new Local();

    use Molajo\Filesystem\Adapter;
    $adapter = new Adapter($handler);


    try {
        $results = $adapter->read('\file\located\here.txt');

    } catch (Exception $e) {
        // deal with the exception
    }

    try {
        $exists = $adapter->exists($path);
    } catch (Exception $e) {
        // deal with the exception
    }

    if ($exists === true) {
			echo 'The file or folder defined in $path does exist.';
    } else {
			echo 'The file or folder defined in $path does NOT exist.';
    }

    try {
        $metadata = $adapter->getMetadata($path);
    } catch (Exception $e) {
        // deal with the exception
    }

    // View all object properties returned


    // Use a single element
    echo $metadata->name;

    try {
        $results = $adapter->read('\file\located\here.txt');
    } catch (Exception $e) {
        // deal with the exception
    }

    $path = '\file\located\here.txt';
    $recursive = true;
    $extension_list = 'txt, pdf, doc';
    $($path, $recursive, $extension_list,
            $

    $path = '\file\located\here.txt';
    $data = 'Write this data.';
    $replace = true;
    $append = false;
    $truncate = false;

    try {
        $results = $adapter->write($path, $data, $replace, $append, $truncate);

    } catch (Exception $e) {
        // deal with the exception
    }

    $path = '\example\of\a\folder';
    $recursive = true;

    try {
        $results = $adapter->delete($path, $recursive);
    } catch (Exception $e) {
        // deal with the exception
    }


    $path                   = '\copy\contents\from\this\folder';
    $target_directory       = '\to\this\folder';
    $target_name            = null;
    $replace            	= true;
    $target_handler = 'Backup';

    try {
        $adapter->copy($path, $target_directory, $target_name, $replace, $target_handler);
    } catch (Exception $e) {
        // deal with the exception
    }


    $path                   = '\move\contents\from\this\folder';
    $target_directory       = '\to\this\folder';
    $target_name            = null;
    $replace            	= false;
    $target_handler = 'Archive';

    try {
        $adapter->move($path, $target_directory, $target_name, $replace, $target_handler);
    } catch (Exception $e) {
        // deal with the exception
    }

    $path                   = '\copy\this\folder\old_name.txt';
    $new_name               = 'new_name.txt';

    try {
        $adapter->rename($path, $new_name);
    } catch (Exception $e) {
        // deal with the exception
    }

    $path        = '\this\folder\';
    $user_name   = 'user';
    $recursive   = true;

    try {
        $adapter->changeOwner($path, $user_name, $recursive);
    } catch (Exception $e) {
        // deal with the exception
    }

    $path        = '\this\folder\';
    $group_id    = 5;
    $recursive   = true;

    try {
        $adapter->changeGroup($path, $group_id, $recursive);
    } catch (Exception $e) {
        // deal with the exception
    }

    $path        = '\this\folder\';
    $permission  = 0755;
    $recursive   = true;

    try {
        $adapter->changePermission($path, $permission, $recursive);
    } catch (Exception $e) {
        // deal with the exception
    }

    $path                = '\copy\this\folder\old_name.txt';
    $modification_time   = $modification_time;
    $access_time         = $access_time;
    $recursive           = false;

    try {
        $adapter->touch($path, $modification_time, $access_time, $recursive);
    } catch (Exception $e) {
        // deal with the exception
    }

    $options = array(
        'source_handler' => 'local',
        'source_path'    => '/x/y/example',
        'target_handler' => 'ftp',
        'target_path'    => '/x/y/backup',
        'archive'        => 'zip'
    );

    $adapter = new \Molajo\Filesystem\File($options);
    $data    = $adapter->backup ();

    curl -s https://getcomposer.org/installer | php

{
    "  "Molajo/Filesystem": "1.*"
    }
}

    php composer.phar install