PHP code example of phossa2 / storage

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

    

phossa2 / storage example snippets


use Phossa2\Storage\Storage;
use Phossa2\Storage\Filesystem;
use Phossa2\Storage\Driver\LocalDriver;

// mount local dir '/www/storage' to '/local'
$storage = new Storage(
    '/local',
    new Filesystem('/www/storage')
);

// add a file
$filename = '/local/newfile.txt';
$storage->put($filename, 'this is the content');

// check existens
if ($storage->has($filename)) {
    // read file content
    $str = $storage->get($filename);

    // delete the file
    $storage->del($filename);
}

// mount another filesystem
$storage->mount('/aws', new Filesystem(new AwsDriver()));

    // get the meta data
    if ($storage->has($file)) {
        $meta = $storage->meta($file);
    }

    // update meta data
    $new = ['mtime' => time()];
    $storage->put($file, null, $new);
    

    // move to another name
    $storage->move('/local/README.txt', '/local/README.bak.txt');

    // copy into another filesystem's directory
    $storage->copy('/local/README.txt', '/aws/www/');
    

  $storage = new Storage(
    '/disk/d',
    new Filesystem(new LocalDriver('D:\\\\'))
  );

  $storage->put('/disk/d/temp/newfile.txt', 'this is content');
  

  // mount as readonly, default is Filesystem::PERM_ALL
  $storage->mount(
      '/readonly',
      new Filesystem(
        '/home/www/public',
        Filesystem::PERM_READ
      )
  );

  // will fail
  $storage->put('/readonly/newfile.txt', 'this is the content');
  

  $driver = new LocalDriver('/home/www/public');

  // writable
  $storage->mount('/public', new Filesystem($driver));

  // readonly
  $storage->mount('/readonly', new Filesystem($driver, Filesystem::PERM_READ));
  

  // mount root
  $storage->mount('/', new Filesystem(...));

  // mount var
  $storage->mount('/var', new Filesystem(...));

  // mount cache
  $storage->mount('/var/cache', new Filesystem(...));
  

  // read stream
  $stream = $storage->get('/local/thefile.txt', true);

  // write with stream
  $storage->put('/local/anotherfile.txt', $stream);

  // close it
  if (is_resource($stream)) {
      fclose($stream);
  }
  

    if (!$storage->copy('/local/from.txt', '/local/to.txt')) {
        $err = $storage->getError();
    }