Code Monkey home page Code Monkey logo

yii2-filesystem's Introduction

Krajee Logo
yii2-filesystem
Donate

Latest Stable Version Latest Unstable Version License Total Downloads Monthly Downloads Daily Downloads

File system utilities for managing files and folders including reading, writing and appending to files. It also includes a Resumable component that provides an object oriented backend to manage resumable and chunk file uploads via resumable.js.

Fork information

The project was forked to use under php 8 and above.

Install

Either run

$ php composer.phar require kartik-v/yii2-filesystem "@dev"

or add

"fikusz01/yii2-filesystem": "*"

to the require section of your composer.json file.

Usage

Example showing creation of a folder instance and search for all the .csv files within it:

use kartik\filesystem\Folder;

$dir = new Folder('/path/to/folder');
$files = $dir->find('.*\.csv');

Now you can loop through the files and read from or write/append to the contents or simply delete the file:

use kartik\filesystem\File;

foreach ($files as $file) {
    $file = new File($dir->pwd() . DIRECTORY_SEPARATOR . $file);
    $contents = $file->read();
    // $file->write('I am overwriting the contents of this file');
    // $file->append('I am adding to the bottom of this file.');
    // $file->delete(); // I am deleting this file
    $file->close(); // Be sure to close the file when you're done
}

Examples

use kartik\filesystem\Folder;
use kartik\filesystem\File;

/**
 * Create a new folder with 0755 permissions
 */
$dir = new Folder('/path/to/folder', true, 0755);

/**
 * Create a new file with 0644 permissions
 */
$file = new File('/path/to/file.php', true, 0644);

/**
 * addPathElement: Returns $path with $element added, with correct slash in-between.
 */
$path = Folder::addPathElement('/a/path/for', 'testing');
// $path equals /a/path/for/testing

/**
 * cd: Change directory to $path. Returns false on failure.
 */
$folder = new Folder('/foo');
echo $folder->path; // Prints /foo
$folder->cd('/bar');
echo $folder->path; // Prints /bar
$false = $folder->cd('/non-existent-folder');

/**
 * chmod: Change the mode on a directory structure recursively. 
 *        This includes changing the mode on files as well.
 */
$dir = new Folder();
$dir->chmod('/path/to/folder', 0644, true, ['skip_me.php']);

/**
 * copy: Recursively copy a directory.
 */
$folder1 = new Folder('/path/to/folder1');
$folder1->copy('/path/to/folder2');
// Will put folder1 and all its contents into folder2

$folder = new Folder('/path/to/folder');
$folder->copy([
    'to' => '/path/to/new/folder',
    'from' => '/path/to/copy/from', // Will cause a cd() to occur
    'mode' => 0755,
    'skip' => ['skip-me.php', '.git'],
    'scheme' => Folder::SKIP  // Skip directories/files that already exist.
]);

/**
 * create: Create a directory structure recursively. Can be used to create 
 *         deep path structures like /foo/bar/baz/shoe/horn
 */
$folder = new Folder();
if ($folder->create('foo' . DS . 'bar' . DS . 'baz' . DS . 'shoe' . DS . 'horn')) {
    // Successfully created the nested folders
}

/**
 * delete: Recursively remove directories if the system allows.
 */
$folder = new Folder('foo');
if ($folder->delete()) {
    // Successfully deleted foo and its nested folders
}

/**
 * find: Returns an array of all matching files in the current directory.
 */
// Find all .png in your webroot/img/ folder and sort the results
$dir = new Folder(WWW_ROOT . 'img');
$files = $dir->find('.*\.png', true);
/*
Array
(
    [0] => cake.icon.png
    [1] => test-error-icon.png
    [2] => test-fail-icon.png
    [3] => test-pass-icon.png
    [4] => test-skip-icon.png
)
*/

/**
 * findRecursive: Returns an array of all matching files in and below the current directory.
 */
// Recursively find files beginning with test or index
$dir = new Folder(WWW_ROOT);
$files = $dir->findRecursive('(test|index).*');
/*
Array
(
    [0] => /var/www/demo/index.php
    [1] => /var/www/demo/test.php
    [2] => /var/www/demo/img/test-skip-icon.png
    [3] => /var/www/demo/img/test-fail-icon.png
    [4] => /var/www/demo/img/test-error-icon.png
    [5] => /var/www/demo/img/test-pass-icon.png
)
*/

/**
 * read: Returns an array of the contents of the current directory. The returned
 *       array holds two sub arrays: One of directories and one of files.
 */
// Recursively find files beginning with test or index
$dir = new Folder(WWW_ROOT);
$files = $dir->findRecursive('(test|index).*');
/*
Array
(
    [0] => /var/www/demo/index.php
    [1] => /var/www/demo/test.php
    [2] => /var/www/demo/img/test-skip-icon.png
    [3] => /var/www/demo/img/test-fail-icon.png
    [4] => /var/www/demo/img/test-error-icon.png
    [5] => /var/www/demo/img/test-pass-icon.png
)
*/

License

yii2-filesystem is released under the BSD-3-Clause License. See the bundled LICENSE.md for details.

yii2-filesystem's People

Contributors

kartik-v avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.