定制 popphp/pop-file 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

popphp/pop-file

最新稳定版本:2.1.0p1

Composer 安装命令:

composer require popphp/pop-file

包简介

Pop File Component for Pop PHP Framework

README 文档

README

END OF LIFE

The pop-file component v2.1.0 is now end-of-life. The upload and dir sub-components have been forked and pushed into other repositories, respectively:

  • popphp/pop-http (now the Pop\Http\Upload class)
  • popphp/pop-dir (now the Pop\Dir\Dir class)

Build Status Coverage Status

OVERVIEW

pop-file is a component for managing file uploads and easily traversing files within a directory. With file uploads, you can set fine-grain controls to manage specific upload parameters like file types and file size. Also, you can traverse directories recursively as well.

pop-fileis a component of the Pop PHP Framework.

INSTALL

Install pop-file using Composer.

composer require popphp/pop-file

BASIC USAGE

File uploads

Basic file upload
use Pop\File\Upload;

$upload = new Upload('/path/to/uploads');
$upload->useDefaults();

$upload->upload($_FILES['file_upload']);

// Do something with the newly uploaded file
if ($upload->isSuccess()) {
    $file = $upload->getUploadedFile();
} else {
    echo $upload->getErrorMessage();
}

The above code creates the upload object, sets the upload path and sets the basic defaults, which includes a max file size of 10MBs, and an array of allowed common file types as well as an array of common disallowed file types.

File upload names and overwrites

By default, the file upload object will not overwrite a file of the same name. In the above example, if $_FILES['file_upload']['name'] is set to 'my_document.docx' and that file already exists in the upload path, it will be renamed to 'my_document_1.docx'.

If you want to enable file overwrites, you can simply do this:

$upload->overwrite(true);

Also, you can give the file a direct name on upload like this:

$upload->upload($_FILES['file_upload'], 'my-custom-filename.docx');

And if you need to check for a duplicate filename first, you can use the checkFilename method. If the filename exists, it will append a '_1' to the end of the filename, or loop through until it finds a number that doesn't exist yet (_#). If the filename doesn't exist yet, it returns the original name.

$filename = $upload->checkFilename('my-custom-filename.docx');

// $filename is set to 'my-custom-filename_1.docx'
$upload->upload($_FILES['file_upload'], $filename);

Directory traversal

Traversing a directory
use Pop\File\Dir;

$dir = new Dir('my-dir');

foreach ($dir->getFiles() as $file) {
    echo $file;
}

If you want to traverse the directory recursively and get the full path of each file.

use Pop\File\Dir;

$dir = new Dir('my-dir', [
    'absolute'  => true,
    'recursive' => true
]);

foreach ($dir->getFiles() as $file) {
    echo $file;
}

The available boolean options for the $options array parameter are:

  • 'absolute' => store the absolute, full path of the items in the directory
  • 'relative' => store the relative path of the items in the directory
  • 'recursive' => traverse the directory recursively
  • 'filesOnly' => store only files in the object (and not other directories)
Emptying a directory
use Pop\File\Dir;

$dir = new Dir('my-dir');
$dir->emptyDir(true);

The true flag will remove the actually directory as well.

统计信息

  • 总下载量: 948
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: New
  • 更新时间: 2014-12-08