dnj/local-filesystem 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dnj/local-filesystem

最新稳定版本:1.0.1

Composer 安装命令:

composer require dnj/local-filesystem

包简介

Local disk implementation of dnj/filesystem

README 文档

README

Latest Version on Packagist Total Downloads Software License Testing status

Introduction

This is a simple implemenetation of DNJ\FileSystem for local disk based file system.

How To Use

This is an implementation of local disk filesystem that allows you to work with local files.
First of all, you need to add this library to your project, so run:

composer require dnj/local-filesystem

File basic usage:

Read file:

<?php
use dnj\Filesystem\Local\File;

$file = new File('/etc/hosts');
$content = $file->read();
echo $content; // prints: 127.0.0.1       localhost 

Write file:

<?php
use dnj\Filesystem\Local\File;

$file = new File('~/adele-hello.txt');

$lyrics = <<<EOF
Hello, it's me
I was wondering if after all these years you'd like to meet
To go over everything
They say that time's supposed to heal ya, but I ain't done much healing
EOF;

try {
    $file->write($lyrics);
} catch (IOException $e) {
    // in case failed for any reason, like permission denied
}

Rename file:

<?php
use dnj\Filesystem\Local\File;


$file = new File('~/adele-hello.txt');
$lyrics = <<<EOF
I heard that you're settled down
That you found a girl and you're married now
I heard that your dreams came true
Guess she gave you things, I didn't give to you
Old friend, why are you so shy?
EOF;

try {
    $file->write($lyrics);
} catch (IOException $e) {
    // in case failed for any reason, like permission denied, ...
}


try {
    $file->rename('adele-someone-like-you.txt');

    // or you can completly move this file to new dest:
    $destFile = new File('/tmp/adele-someone-like-you.txt');
    $file->move($destFile);
} catch (IOException $e) {
    // in case failed for any reason, like permission denied, ...
}

Get size of file:

<?php
use dnj\Filesystem\Local\File;

$file = new File('/etc/hosts');
echo $file->size(); // prints the size of file in bytes

Existence of file and touch it (create it) if not exists:

<?php
use dnj\Filesystem\Local\File;

$file = new File('~/dnj-does-file-exists.txt');
echo 'file ' . ($file->exists() ? 'exists.' : 'not exists!');

if ($file->exists()) {
    echo 'File exists.' . PHP_EOL;
} else {
    echo 'File does not exists!' . PHP_EOL;
    // touch file
    $file->touch();
}

Delete file:

<?php
use dnj\Filesystem\Local\File;

$file = new File('~/test-dnj-delete-file.txt');
$file->write('Hello World!');
$file->delete();

Append to file:

<?php
use dnj\Filesystem\Local\File;

$file = new File('~/hello-world.txt');
$file->write('Hello');
echo $file->read(); // prints: Hello

$file->append(' World!');
echo $file->read(); // prints: Hello World!

Directory basic usage:

Check directory existence:

<?php
use dnj\Filesystem\Local\Directory;

$dir = new Directory('~/dnj/');
$dir->exists(); // return bool

Make directory:

<?php
use dnj\Filesystem\Local\Directory;

$dir = new Directory('~/dnj/');
$dir->make();
// or:
$recursive = true;
$dir->make($recursive, 0700); // make recursivley with permission 0700

Get files of directory:

<?php
use dnj\Filesystem\Local\Directory;

$dir = new Directory('/tmp/');
if ($dir->exists()) {
    // $directory->file() returns Generator of dnj\Filesystem\Local\File;
    $recursive = false;
    foreach ($directory->files($recursive) as $file) {
        echo $file->getPath() . PHP_EOL;
    }
}

Get directories of directory:

<?php
use dnj\Filesystem\Local\Directory;

$parent = new Directory('/tmp/');
if ($dir->exists()) {
    // $directory->directories() returns Generator of dnj\Filesystem\Local\Directory;
    $recursive = false;
    foreach ($parent->directories($recursive) as $directory) {
        echo $directory->getPath() . PHP_EOL;
    }
}

Get directories of directory:

<?php
use dnj\Filesystem\Local\Directory;

$dir = new Directory('/tmp/');
if ($dir->exists()) {
    // $directory->file() returns Generator of dnj\Filesystem\Local\File;
    $recursive = false;
    foreach ($directory->files($recursive) as $file) {
        echo $file->getPath() . PHP_EOL;
    }
}

Get items (files and directories) of directory:

<?php
use dnj\Filesystem\Local\Directory;

$dir = new Directory('/tmp/');
if ($dir->exists()) {
    // $directory->file() returns Generator of dnj\Filesystem\Local\File|dnj\Filesystem\Local\Directory;
    $recursive = false;
    foreach ($directory->items($recursive) as $node) {
        echo $node->getPath() . PHP_EOL;
    }
}

Get size of a directory:

<?php
use dnj\Filesystem\Local\Directory;

$dir = new Directory('/tmp/');
$recursively = true;
echo $dir->size($recursively); // prints the size of directory and all of it's contents

Get file from directory:

<?php
use dnj\Filesystem\Local\Directory;

$parent = new Directory('/tmp/');
$file = $parent->file('test-file.txt');
$file->write('https://dnj.co.ir');

echo $file->getPath(); // prints: /tmp/test-file.txt

Get directory from directory:

<?php
use dnj\Filesystem\Local\Directory;

$parent = new Directory('/tmp/');
$directory = $parent->file('test-directory');
$directory->make();

echo $directory->getPath(); // prints: /tmp/test-directory

Delete directory:

<?php
use dnj\Filesystem\Local\Directory;

$parent = new Directory('/tmp/');
$directory = $parent->file('test-directory');
$directory->make();

echo $directory->getPath(); // prints: /tmp/test-directory

$directory->delete(); // delete directory and all of it's content

About

We'll try to maintain this project as simple as possible, but Pull Requests are welcomed!

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 1.81k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固