atk14/files 问题修复 & 功能扩展

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

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

atk14/files

Composer 安装命令:

composer require atk14/files

包简介

A PHP class providing static methods for reading, writing, copying, moving, and deleting files and directories, MIME type detection, image size detection, and file permission management

README 文档

README

Tests

A PHP class providing static methods for common file system operations: reading and writing files, copying, moving, deleting, MIME type detection, image size detection, finding files, and managing permissions.

Installation

composer require atk14/files

Usage

All methods follow the same error-reporting convention — pass $error and $error_str by reference to check for failures without exceptions:

$content = Files::GetFileContent("/path/to/file", $error, $error_str);
if ($error) {
    echo $error_str; // "file.txt is not a file"
}

Both parameters are optional.

Reading and writing

$content = Files::GetFileContent("/path/to/file");

Files::WriteToFile("/path/to/file", $content);
Files::AppendToFile("/path/to/file", $content);
Files::EmptyFile("/path/to/file");
Files::TouchFile("/path/to/file");

WriteToCacheFile() writes atomically via a temporary file and rename, preventing partial reads in concurrent situations:

Files::WriteToCacheFile("/path/to/cache/file", $content);

Copying, moving and deleting

Files::CopyFile("/path/to/source", "/path/to/target");

// move a file into an existing directory
Files::MoveFile("/path/to/file.jpg", "/path/to/dir/");
// move a file to a new name
Files::MoveFile("/path/to/old.jpg", "/path/to/new.jpg");

Files::Unlink("/path/to/file");
Files::RecursiveUnlinkDir("/path/to/dir");

Directories

Files::Mkdir("/path/to/new/directory");         // creates all missing parent directories
Files::MkdirForFile("/path/to/new/file.txt");   // creates "/path/to/new"

Temporary files

$tmp = Files::WriteToTemp($content);    // write content to a new temp file
$tmp = Files::CopyToTemp("/path/to/source");
// ... work with $tmp ...
Files::Unlink($tmp);

$path = Files::GetTempFilename();                   // just reserve a filename
$path = Files::GetTempFilename("my_prefix_");       // with a custom prefix

echo Files::GetTempDir();   // e.g. "/tmp"

The temporary directory defaults to sys_get_temp_dir(). Define the TEMP constant before including the library to override it:

define("TEMP", "/path/to/temp/");

MIME type detection

$mime = Files::DetermineFileType("/path/to/file");  // e.g. "image/jpeg"

// for uploaded files, pass the original filename to improve detection accuracy
$mime = Files::DetermineFileType(
    $_FILES["file"]["tmp_name"],
    ["original_filename" => $_FILES["file"]["name"]],
    $preferred_suffix  // e.g. "jpg"
);

Detection uses mime_content_type(), finfo, or the file shell command as fallbacks. When Imagick is available it is used as a last resort.

Image dimensions

[$width, $height] = Files::GetImageSize("/path/to/image.jpg");
[$width, $height] = Files::GetImageSizeByContent($binary_image_data);

Finding files

// all files recursively
$files = Files::FindFiles("./dir");

// only immediate children
$files = Files::FindFiles("./dir", ["maxdepth" => 1]);

// filter by name pattern
$images = Files::FindFiles("./dir", ["pattern" => '/\.(png|jpg)$/i']);

// filter by modification time
$recent = Files::FindFiles("./log", [
    "pattern"  => '/\.log$/',
    "min_mtime" => time() - 30 * 60,    // not older than 30 minutes
    "max_mtime" => time(),
]);

Permissions

New files are created with 0666, new directories with 0777 (both subject to the process umask). Override the defaults globally:

define("FILES_DEFAULT_FILE_PERMS", 0640);
define("FILES_DEFAULT_DIR_PERMS", 0750);

Or at runtime:

$prev = Files::SetDefaultFilePerms(0640);
// ... do some work ...
Files::SetDefaultFilePerms($prev);

Apply the current defaults to an existing file or directory:

Files::NormalizeFilePerms("/path/to/file");

Check whether a file is both readable and writable by the current process:

if (!Files::IsReadableAndWritable("/path/to/file")) {
    // ...
}

Validate an HTTP-uploaded file:

if (!Files::IsUploadedFile($_FILES["file"]["tmp_name"])) {
    // reject — not a legitimate upload
}

Licence

Files is free software distributed under the terms of the MIT license

atk14/files 适用场景与选型建议

atk14/files 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 54.42k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 01 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「file」 「filesystem」 「upload」 「mime」 「files」 「permissions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 atk14/files 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 atk14/files 我们能提供哪些服务?
定制开发 / 二次开发

基于 atk14/files 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 54.42k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 5
  • 依赖项目数: 18
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-23