├── .gitignore ├── .idea └── vcs.xml ├── README.md ├── composer.json ├── config └── flysystem.php └── src ├── Config.php ├── Container.php ├── Files.php ├── FilesDist.php ├── adapter ├── Ftp.php ├── Local.php └── Sftp.php └── inter └── AdapterInterface.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | composer.lock 3 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # think-filesystem 2 | think PHP5集成Filesystem 3 | 4 | 5 | think-filesystem基于 Frank de Jonge 开发的 PHP 包 Flysystem 提供了强大的文件系统抽象。think-filesystem 文件系统集成是中文化和简单化,当然也做了一下本地化封装。 6 | 7 | ## 安装 8 | 9 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 10 | 11 | Either run 12 | 13 | ```bash 14 | $ composer require selden1992/think-flysystem 15 | ``` 16 | 17 | or add 18 | 19 | ``` 20 | "selden1992/think-flysystem": "dev-master" 21 | ``` 22 | 23 | to the `require` section of your `composer.json` file. 24 | 25 | ## 配置 26 | 27 | 28 | 复制 /vendor/selden1992/think-flysystem/config/flysystem.php 到 CONF_PATH.'extra/flysystem.php' 29 | 30 | ```php 31 | 32 | 'local', 40 | // 本地驱动 41 | 'local'=>[ 42 | 'adapter_class'=>\Think\flysystem\adapter\Local::class, 43 | // 跟目录 44 | 'root'=>'./files/', 45 | // 权限参数 46 | 'permissions'=>[ 47 | 'file' => [ 48 | 'public' => 0744, 49 | 'private' => 0700, 50 | ], 51 | 'dir' => [ 52 | 'public' => 0755, 53 | 'private' => 0700, 54 | ] 55 | ], 56 | // 目录别名 57 | 'alias'=>[ 58 | 'image'=>'image/user/', 59 | ], 60 | ], 61 | // ftp 扩展 62 | 'ftp'=>[ 63 | 'adapter_class'=>\Think\flysystem\adapter\Ftp::class, 64 | // 权限参数 65 | 'permissions'=>[ 66 | 'host' => 'ftp.example.com', 67 | 'username' => 'username', 68 | 'password' => 'password', 69 | 70 | /** optional config settings */ 71 | 'port' => 21, 72 | 'root' => '/path/to/root', 73 | 'passive' => true, 74 | 'ssl' => true, 75 | 'timeout' => 30, 76 | ], 77 | // 目录别名 78 | 'alias'=>[ 79 | 'image'=>'image/user/', 80 | ], 81 | ], 82 | // sftp 扩展 83 | 'sftp'=>[ 84 | 'adapter_class'=>\Think\flysystem\adapter\Sftp::class, 85 | // 权限参数 86 | 'permissions'=>[ 87 | 'host' => 'example.com', 88 | 'port' => 21, 89 | 'username' => 'username', 90 | 'password' => 'password', 91 | 'privateKey' => 'path/to/or/contents/of/privatekey', 92 | 'root' => '/path/to/root', 93 | 'timeout' => 10, 94 | ], 95 | // 目录别名 96 | 'alias'=>[ 97 | 'image'=>'image/user/', 98 | ], 99 | ], 100 | ]; 101 | 102 | ``` 103 | 如果ftp提示不能写,可以设置一个777权限的目录调试是否权限影响到 104 | ```php 105 | putenv(‘TMPDIR=/Users/cyz/web/test’); 106 | ``` 107 | 108 | ## 普通使用 109 | 110 | 111 | ```php 112 | 113 | put('test2.log',time()); 126 | // 指定驱动 127 | Files::disk('local')->put('test3.log',time()); 128 | 129 | // 普通读取 130 | echo Files::read('log/test.log'); 131 | // 目录别名读取 132 | echo Files::alias('log_alias')->read('test2.log'); 133 | 134 | // 复制 135 | Files::copy('log/test.log','log/image1.log'); 136 | // 第二参数路径使用别名 137 | Files::copy('log/test.log',['alias'=>'local','path'=>'image1.log']); 138 | 139 | // 获取别名真实路径 140 | echo Files::getAliasPath('image'); 141 | 142 | /** 143 | * ftp 操作 144 | */ 145 | Files::disk('ftp')->put('log/test4.log',time()); 146 | echo Files::disk('ftp')->alias('log_alias')->read('test4.log'); 147 | 148 | /** 149 | * sftp 操作 150 | * 需要按 composer require league/flysystem-sftp 151 | */ 152 | Files::disk('sftp')->put('log/test4.log',time()); 153 | echo Files::disk('sftp')->alias('log_alias')->read('test4.log'); 154 | } 155 | } 156 | 157 | ``` 158 | 159 | alias目录别名的作用在于快捷更换目录,例如开发阶段,log_alias指向root/log/下,部署阶段更改为 web/log/ 160 | 161 | ## 其他使用方法和flysystem一致 162 | 163 | API 164 | 一般用法 165 | 166 | 写文件 167 | ```php 168 | Files::write('path/to/file.txt', 'contents'); 169 | ``` 170 | 171 | 更新文件 172 | ```php 173 | Files::update('path/to/file.txt', 'new contents'); 174 | ``` 175 | 写或更新文件 176 | ```php 177 | Files::put('path/to/file.txt', 'contents'); 178 | ``` 179 | 读取文件 180 | ```php 181 | $contents = Files::read('path/to/file.txt'); 182 | ``` 183 | 检查文件是否存在 184 | ```php 185 | $exists = Files::has('path/to/file.txt'); 186 | ``` 187 | 注意:这只对文件而不是目录具有一致的行为。在Flysystem中,目录不太重要,它们是隐式创建的,常常被忽略,因为不是每个适配器(文件系统类型)都支持目录。 188 | 189 | 删除文件 190 | ```php 191 | Files::delete('path/to/file.txt'); 192 | ``` 193 | 读取和删除 194 | ```php 195 | $contents = Files::readAndDelete('path/to/file.txt'); 196 | ``` 197 | 重命名文件 198 | ```php 199 | Files::rename('filename.txt', 'newname.txt'); 200 | ``` 201 | 复制文件 202 | ```php 203 | Files::copy('filename.txt', 'duplicate.txt'); 204 | ``` 205 | 获取Mimetypes 206 | ```php 207 | $mimetype = Files::getMimetype('path/to/file.txt'); 208 | ``` 209 | 获取时间戳 210 | ```php 211 | $timestamp = Files::getTimestamp('path/to/file.txt'); 212 | ``` 213 | 获取文件大小 214 | ```php 215 | $size = Files::getSize('path/to/file.txt'); 216 | ``` 217 | 创建目录 218 | ```php 219 | Files::createDir('path/to/nested/directory'); 220 | ``` 221 | 当写入更深的路径时,也隐含地指定了目录 222 | ```php 223 | Files::write('path/to/file.txt', 'contents'); 224 | ``` 225 | 删除目录 226 | ```php 227 | Files::deleteDir('path/to/directory'); 228 | ``` 229 | 上述方法将递归删除目录 230 | 231 | 注意:Flysystem API使用的所有路径都是相对于适配器根目录的。 232 | 233 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "selden1992/think-flysystem", 3 | "description": "thinkphp 集成的文件系统,本地存储,第三发存储提供一致的API", 4 | "type": "think-extension", 5 | "keywords": [ 6 | "thinkphp", 7 | "flysystem", 8 | "filesystem", 9 | "files", 10 | "aws", 11 | "s3", 12 | "azure", 13 | "copy.com", 14 | "dropbox", 15 | "ftp", 16 | "gridfs", 17 | "rackspace", 18 | "sftp", 19 | "webdav" 20 | ], 21 | "license": "Apache-2.0", 22 | "authors": [ 23 | { 24 | "name": "selden1992", 25 | "email": "2206582181@qq.com" 26 | } 27 | ], 28 | "require": { 29 | "php": ">=5.6.4", 30 | "league/flysystem": "^1.0" 31 | }, 32 | "autoload": { 33 | "psr-4": { 34 | "Think\\flysystem\\": "src" 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /config/flysystem.php: -------------------------------------------------------------------------------- 1 | 'local', 14 | // 本地驱动 15 | 'local'=>[ 16 | 'adapter_class'=>\Think\flysystem\adapter\Local::class, 17 | // 跟目录 18 | 'root'=>'./files/', 19 | // 权限参数 20 | 'permissions'=>[ 21 | 'file' => [ 22 | 'public' => 0744, 23 | 'private' => 0700, 24 | ], 25 | 'dir' => [ 26 | 'public' => 0755, 27 | 'private' => 0700, 28 | ] 29 | ], 30 | // 目录别名 31 | 'alias'=>[ 32 | 'image'=>'image/user/', 33 | ], 34 | ], 35 | // ftp 扩展 36 | 'ftp'=>[ 37 | 'adapter_class'=>\Think\flysystem\adapter\Ftp::class, 38 | // 权限参数 39 | 'permissions'=>[ 40 | 'host' => 'ftp.example.com', 41 | 'username' => 'username', 42 | 'password' => 'password', 43 | 44 | /** optional config settings */ 45 | 'port' => 21, 46 | 'root' => '/path/to/root', 47 | 'passive' => true, 48 | 'ssl' => true, 49 | 'timeout' => 30, 50 | ], 51 | // 目录别名 52 | 'alias'=>[ 53 | 'image'=>'image/user/', 54 | ], 55 | ], 56 | // sftp 扩展 57 | 'sftp'=>[ 58 | 'adapter_class'=>\Think\flysystem\adapter\Sftp::class, 59 | // 权限参数 60 | 'permissions'=>[ 61 | 'host' => 'example.com', 62 | 'port' => 21, 63 | 'username' => 'username', 64 | 'password' => 'password', 65 | 'privateKey' => 'path/to/or/contents/of/privatekey', 66 | 'root' => '/path/to/root', 67 | 'timeout' => 10, 68 | ], 69 | // 目录别名 70 | 'alias'=>[ 71 | 'image'=>'image/user/', 72 | ], 73 | ], 74 | ]; -------------------------------------------------------------------------------- /src/Config.php: -------------------------------------------------------------------------------- 1 | FilesDist = $FilesDist; 21 | } 22 | 23 | /** 24 | * 自动调用 25 | * 26 | * @param $name 27 | * @param $arguments 28 | * @return mixed 29 | */ 30 | public function __call($name, $arguments) 31 | { 32 | if($this->alias){ 33 | $arguments[0] = $this->alias.$arguments[0]; 34 | } 35 | if(isset($arguments[1]['alias'])){ 36 | $config = $this->FilesDist->config; 37 | if(isset($config['alias'][$arguments[1]['alias']])){ 38 | $arguments[1] = $config['alias'][$arguments[1]['alias']].$arguments[1]['path']; 39 | }else{ 40 | $arguments[1] = $arguments[1]['alias']; 41 | } 42 | } 43 | return call_user_func_array(array($this->FilesDist, $name), $arguments); 44 | } 45 | 46 | /** 47 | * 目录别名 48 | * 49 | * @param $name 50 | * @return mixed 51 | */ 52 | public function alias($name) 53 | { 54 | $config = $this->FilesDist->config; 55 | if(isset($config['alias'][$name])){ 56 | $this->alias = $config['alias'][$name]; 57 | }else{ 58 | $this->alias = $name; 59 | } 60 | return $this; 61 | } 62 | 63 | /** 64 | * 获取别名真实路径 65 | * 66 | * @param $name 67 | */ 68 | public function getAliasPath($name) 69 | { 70 | $config = $this->FilesDist->config; 71 | if(isset($config['alias'][$name])){ 72 | $path = $config['alias'][$name]; 73 | }else{ 74 | $path = $name; 75 | } 76 | return $path; 77 | } 78 | } -------------------------------------------------------------------------------- /src/Files.php: -------------------------------------------------------------------------------- 1 | config = $config; 22 | $adapter = new $config['adapter_class'](); 23 | $adapter = $adapter->makeDisk($config); 24 | $filesystem = new Filesystem($adapter); 25 | $this->filesystem = $filesystem; 26 | } 27 | 28 | /** 29 | * 写文件 30 | * 31 | * @param $file 32 | * @param $contents 33 | */ 34 | public function write($file,$contents) 35 | { 36 | $this->filesystem->write($file,$contents); 37 | } 38 | 39 | /** 40 | * 更新文件 41 | * 42 | * @param $file 43 | * @param $contents 44 | */ 45 | public function update($file,$contents) 46 | { 47 | $this->filesystem->update($file,$contents); 48 | } 49 | 50 | /** 51 | * 写或更新文件 52 | * 53 | * @param $file 54 | * @param $contents 55 | */ 56 | public function put($file,$contents) 57 | { 58 | $this->filesystem->put($file,$contents); 59 | } 60 | 61 | /** 62 | * 获取文件内容 63 | * 64 | * @param $file 65 | * @return bool|false|string 66 | */ 67 | public function read($file) 68 | { 69 | return $this->filesystem->read($file); 70 | } 71 | 72 | /** 73 | * 检查文件是否存在 74 | * 75 | * @param $file 76 | * @return bool 77 | */ 78 | public function has($file) 79 | { 80 | return $this->filesystem->has($file); 81 | } 82 | 83 | /** 84 | * 删除文件 85 | * 86 | * @param $path 87 | */ 88 | public function delete($path) 89 | { 90 | $this->filesystem->delete($path); 91 | } 92 | 93 | /** 94 | * 读取和删除 95 | * 96 | * @param $path 97 | * @return bool|false|string 98 | */ 99 | public function readAndDelete($path) 100 | { 101 | return $this->filesystem->readAndDelete($path); 102 | } 103 | 104 | /** 105 | * 重命名 106 | * 107 | * @param $path 108 | */ 109 | public function rename($path) 110 | { 111 | $this->filesystem->rename($path); 112 | } 113 | 114 | /** 115 | * 复制文件 116 | * 117 | * @param $path 118 | * @param $toPath 119 | */ 120 | public function copy($path,$toPath) 121 | { 122 | $this->filesystem->copy($path,$toPath); 123 | } 124 | 125 | /** 126 | * 获取Mimetypes 127 | * 128 | * @param $path 129 | * @return bool|false|string 130 | */ 131 | public function getMimetype($path) 132 | { 133 | return $this->filesystem->getMimetype($path); 134 | } 135 | 136 | /** 137 | * 时间戳 138 | * @param $path 139 | * @return bool|false|string 140 | */ 141 | public function getTimestamp($path) 142 | { 143 | return $this->filesystem->getMimetype($path); 144 | } 145 | 146 | /** 147 | * 获取文件大小 148 | * 149 | * @param $path 150 | * @return bool|false|int 151 | */ 152 | public function getSize($path) 153 | { 154 | return $this->filesystem->getSize($path); 155 | } 156 | 157 | /** 158 | * 创建目录 159 | * 160 | * @param $path 161 | * @internal param $arrParameter 162 | */ 163 | public function createDir($path) 164 | { 165 | $this->filesystem->createDir($path); 166 | } 167 | 168 | /** 169 | * 递归删除目录 170 | * 171 | * @param $path 172 | * @return bool 173 | */ 174 | public function deleteDir($path) 175 | { 176 | $this->filesystem->deleteDir($path); 177 | } 178 | 179 | } -------------------------------------------------------------------------------- /src/adapter/Ftp.php: -------------------------------------------------------------------------------- 1 | [ 35 | 'public' => 0744, 36 | 'private' => 0700, 37 | ], 38 | 'dir' => [ 39 | 'public' => 0755, 40 | 'private' => 0700, 41 | ] 42 | ]); 43 | return $adapter; 44 | } 45 | } -------------------------------------------------------------------------------- /src/adapter/Sftp.php: -------------------------------------------------------------------------------- 1 |