SimpleDirLister 是一个 PHP 库,也是这些库的前端。您可以获取完整的文件树和文件信息。您还可以以清晰的方式显示这些信息

这个包的规范存储库似乎已消失,因此该包已被冻结。

0.4.0 2018-03-16 19:35 UTC

This package is auto-updated.

Last update: 2022-01-12 21:50:19 UTC


README

common/io 是一个简单而强大的 I/O 库。它将流行的 Flysystem 包装成 oo 结构,并添加了有用的工具。

<?php
use common\io\File;
use common\io\Manager;
use League\Flysystem\WebDAV\WebDAVAdapter;
use Sabre\DAV\Client;

$webdav = new class(".") extends File {
	public function __construct($dir) {
		$this->defaultUrl = ["scheme" => "dav"];
		$this->workingDir = "/";
		parent::__construct($dir);

		Manager::addAdapter(
			$this->getProtocol(),
			new WebDAVAdapter(
				new Client(
					[
						'baseUri'  => 'https://owncloud.domain.tld/',
						'userName' => 'user',
						'password' => '...'
					]
				), "/remote.php/webdav/"
			)
		);
	}
};

$local = new File(".");
$local
    ->get("README.md")
    ->copy($webdav->mkdir("testDir"));

安装

运行:composer require common-libs/io

像往常一样,在主文件中加载 composer:require_once("vendor/autoload.php");

使用它

使用它非常简单。只需从 common\io\File 初始化一个新的 PHP 对象。

<?php
use common\io\File;
  
$test = new File(".");

列出所有文件和目录

<?php
use common\io\File;
  
$test = new File(".");
foreach ($test->listContents() as $listContent) {
   echo $listContent->getPath();
}

获取目录结构的文件内容

<?php
use common\io\File;

$test = new File(".");
foreach ($test->get("vendor/bin")->listContents() as $listContent) {
  echo $listContent->getPath();
}

commons\io\Directory 实现 Countable, IteratorAggregate, ArrayAccess,因此可以简化

<?php
use common\io\File;

$test = new File(".");
foreach ($test->get("vendor/bin") as $listContent) {
 echo $listContent->getPath();
}
<?php
use common\io\File;

$test = new File(".");
foreach ($test["vendor"]["bin"] as $listContent) {
    echo $listContent->getPath();
}

扩展此类可让您使用任何 flysystem 适配器

<?php
use common\io\File;
use common\io\Manager;
use League\Flysystem\Adapter\Ftp as Adapter;

class Ftp extends File {
	public function __construct($dir) {
		$this->defaultUrl = ["scheme" => "ftp"];
		$this->workingDir = "/";
		
		parent::__construct($dir);
		Manager::addAdapter(
			$this->getProtocol(),
			new Adapter(
				[
					'host'    => 'speedtest.tele2.net',
					'port'    => 21,
					'root'    => '/',
					'passive' => true,
					'ssl'     => false,
					'timeout' => 30,
				]
			)
		);
	}
}

协议/方案用作类内部虚拟映射和标识符。

请参阅 Documentation 了解可用方法的完整列表。

示例

<?php

use common\io\File;
use common\io\Manager;

$local = new File("."); //current dir in vendor/common-libs/io/

$file = $local->createFile("test", "hi"); // create file "test" with content hi
echo $file->getContent() . PHP_EOL; // hi
echo $file->md5() . PHP_EOL; // 49f68a5c8493ec2c0bf489821c21fc3b

/* list all files and dirs recursive and prints their paths & if an file "composer.json" is found more infos are printed */
foreach ($local->listContents() as $listContent) { 
	echo $listContent->getPath() . PHP_EOL;
	if ($listContent->isFile()) {
		if ($listContent->getName() == "composer.json") {
			echo "size: " . $listContent->getSize() . PHP_EOL;
			echo json_decode($listContent->getContent());
		}
	}
}

$lib = $local->get("lib"); // change to dir "lib"

/* list all files and dirs recursive and prints their paths */
foreach ($lib->listContents() as $listContent) {
	echo $listContent->getPath() . PHP_EOL;
}

$local = $lib->getParent(); //redundant, just for demonstration

/* using php7 to get a new ftp object */
$ftp = new class(".") extends File {
	public function __construct($dir) {
		$this->defaultUrl = ["scheme" => "ftp"];
		$this->workingDir = "/";
        		
		parent::__construct($dir);
		
		Manager::addAdapter(
			$this->getProtocol(),
			new League\Flysystem\Adapter\Ftp(
				[
					'host'    => 'speedtest.tele2.net',
					'port'    => 21,
					'root'    => '/',
					'passive' => true,
					'ssl'     => false,
					'timeout' => 30,
				]
			)
		);
	}
};

/* copy "100KB.zip" on ftp server to local dir "testDirRANDOMNUMBER" */
$kbFile    = $ftp->get("100KB.zip")->copy(
	$local->mkdir("testDir" . random_int(0, 9999999999))
);
$randomDir = $kbFile->delete(); // delete downloaded file

许可

GNU GPL v3

Copyright (C) 2017  Profenter Systems

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License 
along with this program. If not, see <https://gnu.ac.cn/licenses/>.