joshmvc / filehandler
PHP库,用于帮助管理文件处理,特别是快速和方便地上传文件
This package is not auto-updated.
Last update: 2024-09-29 05:55:14 UTC
README
FileHandler是一个PHP库,帮助简化文件处理(上传)的过程。它适用于单个和多个文件的上传。
安装
推荐方法:使用Composer通过运行以下脚本获取库
composer require joshmvc/filehandler
其他安装方法
下载zip文件并将其解压到您的目录
或者直接从GitHub克隆到您的库目录。
使用SSH克隆
git@github.com:<your_username>/FileHandler.git
使用HTTPS克隆
https://github.com/<your_username>/FileHandler.git
使用要求
如果手动安装,请将此行代码添加到您的自动加载器中或您想要使用FileHandler的文件中
require_once '<_your_path>/FileHandler/src/FileHandler.php';
如果使用Composer安装,请将此行代码添加到您想要使用FileHandler的文件中
require 'vendor/autoload.php';
在您想要使用此库的PHP文件中,使用以下代码导入命名空间
use JoshMVC\Libs\FileHandler as FileHandler;
新方法
upload_image($files, $destination, $file_name="", $max_size=0)
此方法可用于上传多种格式的图片。
支持的图片格式包括jpeg, jpg, jpe, png, gif, bmp, svg, cod, ief, jfif, tif, tiff, ras, cmx, ico, pnm, pbm, ppm, rgb, xbm, xwd
Both $file_name 和 $max_size 都是可选的。但是,如果您希望指定文件的最终名称,请在第三个参数的空间中插入您的参数,如果您希望指定文件大小,请将大小限制作为字节插入第四个参数的空间中(例如,2MB是210241024字节,因此您将输入2097152)。
示例用法
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)
此方法可用于上传多种格式的视频。
支持的视频格式包括mp2, mp4, avi, mov, mpa, mpe, mpeg, mpg, mpv2, qt, lsf, lsx, asf, asr, asx, movie
Both $file_name 和 $max_size 都是可选的。但是,如果您希望指定文件的最终名称,请在第三个参数的空间中插入您的参数,如果您希望指定文件大小,请将大小限制作为字节插入第四个参数的空间中(例如,2MB是210241024字节,因此您将输入2097152)。
示例用法
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)
此方法可用于上传多种格式的音频。
支持的音频格式包括au, snd, mid, rmi, mp3, aif, aifc, aiff, m3u, ra, ram, wav
Both $file_name 和 $max_size 都是可选的。但是,如果您希望指定文件的最终名称,请在第三个参数的空间中插入您的参数,如果您希望指定文件大小,请将大小限制作为字节插入第四个参数的空间中(例如,2MB是210241024字节,因此您将输入2097152)。
示例用法
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)
此方法可用于上传多种文档格式。
支持的文档格式包括txt, doc, docx, xls, xlsx, ppt, pptx, rtf, pdf和LaTeX文件
Both $file_name 和 $max_size 都是可选的。但是,如果您希望指定文件的最终名称,请在第三个参数的空间中插入您的参数,如果您希望指定文件大小,请将大小限制作为字节插入第四个参数的空间中(例如,2MB是210241024字节,因此您将输入2097152)。
示例用法
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)
此方法可用于上传多种压缩文件类型。
支持的压缩文件格式包括zip, rar, gz, z, gtar, tgz
Both $file_name 和 $max_size 都是可选的。但是,如果您希望指定文件的最终名称,请在第三个参数的空间中插入您的参数,如果您希望指定文件大小,请将大小限制作为字节插入第四个参数的空间中(例如,2MB是210241024字节,因此您将输入2097152)。
示例用法
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
旧方法
__upload($files, $destination, $constraints=[])
__upload方法接受三个参数。
-
$files (必需) - 这包含来自html标签
<input type="file" name="input_name[]" multiple>中的 $_FILES["input_name"]的文件元素 -
$destination (必需) - 这是要保存文件的文件夹的路径。[注意:这不应是url链接。] 例如: "../images/dp/" 或 "../images/dp"
-
$constraints (可选) - 这是基于您的要求上传文件所需的约束。这是一个关联数组,可以忽略任何键值对。约束键
- size_limit: 这是文件大小的限制。[100kb的大小限制是100*1024 = 102400]
- 接受格式:这是一个接受文件类型的数组[一些接受的图像格式为["image/jpeg", "image/png", "image/gif"]];
- 扩展名:这是要保存为最终文件扩展名的字符串。
- 文件名:这是要保存的文件的静态名称。[注意:只有当上传的文件是一个时才有用]
- //此处将添加其他内容...
示例
$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" ];
用例
您想让用户能够上传多个文件到您的服务器。以下是上传规则
- 文件大小不能超过2MB(即2097152字节)
- 文件必须是jpg、jpeg、gif或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); }
常见问题解答
为什么我得到响应 "文件上传失败?"
如果此消息持续出现,您可能已经超过了php.ini文件的上传大小限制。您需要增加它或将以下代码插入到您的HTACCESS文件中。
php_value upload_max_filesize 100M
php_value post_max_size 102M
或者转到php.ini文件中的以下行并编辑以满足您的需求。
; Maximum allowed size for uploaded files. upload_max_filesize = 100M ; Must be greater than or equal to upload_max_filesize post_max_size = 102M
贡献
如果您有兴趣为此项目做出贡献,您可以查看问题了解此项目用户的要求。
然而,如果您有一个新想法,您可以发送邮件到okjool2012@gmail.com与我讨论。谢谢