tigron/skeleton-file

Tigron 文件工具

v2.0.5 2023-02-01 09:52 UTC

README

描述

这个库使得处理用户上传的文件或由您的应用程序创建的文件变得容易。文件将存储在磁盘上,元数据存储在数据库中。这个库会自动以结构化的方式在磁盘上存储文件,并使用唯一的文件名。

安装

通过 composer 安装

composer require tigron/skeleton-file

在您的数据库中创建一个新表

CREATE TABLE `file` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `path` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `md5sum` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
    `mime_type` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
    `size` bigint(20) NOT NULL,
    `expiration_date` datetime DEFAULT NULL,
    `created` datetime NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

如何操作

初始化文件存储

\Skeleton\File\Config::$file_path = $some_very_cool_path;

/**
 * \Skeleton\File\Config::$store_dir is deprecated
 * $store_dir added directory 'file' to the defined path
 *
 * \Skeleton\File\Config::$file_dir is deprecated
 *
 * Please use $file_path instead
 */

上传文件

$file = \Skeleton\File\File::upload($_FILES['upload']);

创建新文件

$file = \Skeleton\File\File::store('filename.txt', 'this is the content');

复制文件

$file2 = $file->copy();

删除文件

$file->delete();

获取文件内容

$contents = $file->get_contents();

获取文件在磁盘上的路径

$path = $file->get_path();

将文件发送到浏览器(下载)

$file->client_download();

通过其 ID 获取文件

$file = File::get_by_id(1);