joshmvc/filehandler 问题修复 & 功能扩展

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

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

joshmvc/filehandler

Composer 安装命令:

composer require joshmvc/filehandler

包简介

PHP Library to help manage file processing especially upload faster and easily

README 文档

README

FileHandler is a php library that helps simplify the process of file processing (upload). It works well with single and multiple file(s) upload.

Installation

Recommended Method: Use Composer to get the library by running the script below

composer require joshmvc/filehandler

Other Installation Methods:

Download zip and extract to your directory

github download

OR clone from github directly to your library directory.

Clone using SSH:

git@github.com:<your_username>/FileHandler.git

Clone using HTTPS:

https://github.com/<your_username>/FileHandler.git

Usage Requirement(s)

If manually installed, add this line of code to your autoloader or to the file you want to use FileHandler in:

require_once '<_your_path>/FileHandler/src/FileHandler.php';

If installed using composer, add this line of code to the file you want to use FileHandler in:

require 'vendor/autoload.php';

Inside the php file you want to use this library, import the namespace using the following code:

use JoshMVC\Libs\FileHandler as FileHandler;

New Method(s)

upload_image($files, $destination, $file_name="", $max_size=0)

This method can be used to upload many format of image(s).

Accepted image format include jpeg, jpg, jpe, png, gif, bmp, svg, cod, ief, jfif, tif, tiff, ras, cmx, ico, pnm, pbm, ppm, rgb, xbm, xwd

Both $file_name and $max_size are optional. However, if you wish to specify final name of file, insert your parameter in the third args space and if you wish to specify file size please do so by inserting the size limit as bytes in the fourth args space (e.g. 2MB is 210241024 bytes so you will input 2097152).

Example Usage:

FileHandler::upload_image($_FILES["img_upload"], "myfiles/images/"); // Saves file as random name and uses the file's extension.
// OR
FileHandler::upload_image($_FILES["img_upload"], "myfiles/images/", "user1.jpg"); // Specify file name
// OR
FileHandler::upload_image($_FILES["img_upload"], "myfiles/images/", "user1.jpg", 2097152); // Specify file size limit
// OR - to add size limit without specifying name, use empty string for the third parameter:
FileHandler::upload_image($_FILES["img_upload"], "myfiles/images/", "", 2097152); // Specify file size limit

upload_video($files, $destination, $file_name="", $max_size=0)

This method can be used to upload many format of video(s).

Accepted video format include mp2, mp4, avi, mov, mpa, mpe, mpeg, mpg, mpv2, qt, lsf, lsx, asf, asr, asx, movie

Both $file_name and $max_size are optional. However, if you wish to specify final name of file, insert your parameter in the third args space and if you wish to specify file size please do so by inserting the size limit as bytes in the fourth args space (e.g. 2MB is 210241024 bytes so you will input 2097152).

Example Usage:

FileHandler::upload_video($_FILES["vid_upload"], "myfiles/videos/"); // Saves file as random name and uses the file's extension.
// OR
FileHandler::upload_video($_FILES["vid_upload"], "myfiles/videos/", "video1.mp4"); // Specify file name
// OR
FileHandler::upload_video($_FILES["vid_upload"], "myfiles/videos/", "video1.mp4", 2097152); // Specify file size limit
// OR - to add size limit without specifying name, use empty string for the third parameter:
FileHandler::upload_video($_FILES["vid_upload"], "myfiles/videos/", "", 2097152); // Specify file size limit

upload_audio($files, $destination, $file_name="", $max_size=0)

This method can be used to upload many format of audio(s).

Accepted audio format include au, snd, mid, rmi, mp3, aif, aifc, aiff, m3u, ra, ram, wav

Both $file_name and $max_size are optional. However, if you wish to specify final name of file, insert your parameter in the third args space and if you wish to specify file size please do so by inserting the size limit as bytes in the fourth args space (e.g. 2MB is 210241024 bytes so you will input 2097152).

Example Usage:

FileHandler::upload_audio($_FILES["aud_upload"], "myfiles/audio/"); // Saves file as random name and uses the file's extension.
// OR
FileHandler::upload_audio($_FILES["aud_upload"], "myfiles/audio/", "audio1.mp3"); // Specify file name
// OR
FileHandler::upload_audio($_FILES["aud_upload"], "myfiles/audio/", "audio1.mp3", 2097152); // Specify file size limit
// OR - to add size limit without specifying name, use empty string for the third parameter:
FileHandler::upload_audio($_FILES["aud_upload"], "myfiles/audio/", "", 2097152); // Specify file size limit

upload_document($files, $destination, $file_name="", $max_size=0)

This method can be used to upload many document format.

Accepted document format include txt, doc, docx, xls, xlsx, ppt, pptx, rtf, pdf and Latex files

Both $file_name and $max_size are optional. However, if you wish to specify final name of file, insert your parameter in the third args space and if you wish to specify file size please do so by inserting the size limit as bytes in the fourth args space (e.g. 2MB is 210241024 bytes so you will input 2097152).

Example Usage:

FileHandler::upload_document($_FILES["doc_upload"], "myfiles/docs/"); // Saves file as random name and uses the file's extension.
// OR
FileHandler::upload_document($_FILES["doc_upload"], "myfiles/docs/", "DOC_123232.pdf"); // Specify file name
// OR
FileHandler::upload_document($_FILES["doc_upload"], "myfiles/docs/", "DOC_123232.pdf", 2097152); // Specify file size limit
// OR - to add size limit without specifying name, use empty string for the third parameter:
FileHandler::upload_document($_FILES["doc_upload"], "myfiles/docs/", "", 2097152); // Specify file size limit

upload_compressed($files, $destination, $file_name="", $max_size=0)

This method can be used to upload many compressed files types.

Accepted compressed file format include zip, rar, gz, z, gtar, tgz

Both $file_name and $max_size are optional. However, if you wish to specify final name of file, insert your parameter in the third args space and if you wish to specify file size please do so by inserting the size limit as bytes in the fourth args space (e.g. 2MB is 210241024 bytes so you will input 2097152).

Example Usage:

FileHandler::upload_compressed($_FILES["archive_upload"], "myfiles/compressed/"); // Saves file as random name and uses the file's extension.
// OR
FileHandler::upload_compressed($_FILES["archive_upload"], "myfiles/compressed/", "my_archive.rar"); // Specify file name
// OR
FileHandler::upload_compressed($_FILES["archive_upload"], "myfiles/compressed/", "my_archive.rar", 2097152); // Specify file size limit
// OR - to add size limit without specifying name, use empty string for the third parameter:
FileHandler::upload_compressed($_FILES["archive_upload"], "myfiles/compressed/", "", 2097152); // Specify file size limit

Legacy Method(s)

__upload($files, $destination, $constraints=[])

__upload method takes in three arguments.

  1. $files (required) - this contains the file element comming straight from $_FILES["input_name"] in html tag <input type="file" name="input_name[]" multiple>

  2. $destination (required) - this is the path to the folder where the file is saved. [NOTE: this should not be url link.] example: "../images/dp/" or "../images/dp"

  3. $constraints (optional) - this is the constraints required for file to be uploaded based on your requirements. This is an associative array in which any key - value pair can be ignored. constraint keys:

    • size_limit: this is the limit of the file in bytes. [size limit for 100kb is 100*1024 = 102400]
    • accepted_format: this is an array of accepted file types [some accepted format for images are ["image/jpeg", "image/png", "image/gif"]];
    • extension: this is the string of the final file extension to be saved as.
    • file_name: this is the static name of the file to be saved. [NOTE: ONLY USEFUL WHEN FILE UPLOADED IS ONE]
    • //others would be added here....

    EXAMPLE:

    $constraints = [
    	"size_limit" => 0, //value in bytes. 100kb = 100*1024
    	"accepted_format" => ["image/jpeg", "image/png", "image/gif"], //must always be array.
    	"extension" => "jpg",
    	"file_name" => "filename.ext"
    ];

Use Case

You want users to be able to upload multiple files to your server. Below are the upload rules

  • file size must not be greater than 2MB (i.e. 2097152 bytes)
  • file must be of image format jpg, jpeg, gif or png

files_upload.html

<form method="POST" action="upload.php" enctype="multipart/form-data">
	<input type="file" name="files[]" multiple />
	<button type="submit">Upload Files</button>
</form>

upload.php

<?php

//Require "vendor/autoload" OR "<your_library_path>/FileHandler/src/FileHandler.php" before using namespace.

use JoshMVC\Libs\FileHandler as FileHandler;

if (isset($_FILES["files"])) {
	$destination = "uploads/";
	$constraints = [
		"size_limit" => 2097152, /* Size Limit of 2MB */
		"accepted_format" => ["image/jpeg", "image/jpg", "image/png", "image/gif"], /* only image format accepted */
	];

	$feedback = FileHandler::__upload($_FILES["files"], $destination, $constraints);
}

FAQ

Why am I getting response "File upload failed?"

If this message persist, you might have exceeded the upload size limit of your php.ini file. You need to increase it or insert the code below to your HTACCESS file.

php_value upload_max_filesize 100M
php_value post_max_size 102M

Or go to the following line in php.ini file and edit to suit your need.

; Maximum allowed size for uploaded files.
upload_max_filesize = 100M

; Must be greater than or equal to upload_max_filesize
post_max_size = 102M

Contributing

If you're interested in contributing to this project, you can check out the issues to know what is being requested by the users of this project.

However, if you're bringing on a new idea, you can mail me at okjool2012@gmail.com to discuss it with me. Thanks

joshmvc/filehandler 适用场景与选型建议

joshmvc/filehandler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 05 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-05-21