Prior to installing support package
get the Composer dependency manager for PHP because it'll simplify installation.
composer require tamedevelopers/file
Step 1 — Composer Instantiate class using
:
require_once __DIR__ . '/vendor/autoload.php';
use Tamedevelopers\File\File;
$file = new File();
require_once __DIR__ . '/vendor/autoload.php';
$file = new Tamedevelopers\File\File();
- To use s3 first require
composer require aws/aws-sdk-php
- By default Aws load entire SDK we won't be needing
- Copy the below code into your root
composer.json
and then run composer update
in terminal.
"scripts": {
"pre-autoload-dump": "Aws\\Script\\Composer\\Composer::removeUnusedServices"
},
"extra": {
"aws/aws-sdk-php": [
"Ec2",
"CloudWatch"
]
}
- Configure The Global Config, so you don't have to always include default settings
Key |
Types |
Description |
message |
Assoc array |
Create all error messages |
config |
Assoc array |
Create all needed config data |
class |
Assoc array |
Create error and success class |
config_file(
message: [
'401' => 'Select file to upload',
'402' => 'File upload is greater than allowed size of:',
'403' => 'Maximum file upload exceeded. Limit is:',
'404' => 'Uploaded file format not allowed. Allowed formats:',
'405' => 'Image dimension allowed is:',
'405x' => 'Image dimension should be greater than or equal to:',
'200' => 'File uploaded successfully:',
'kb' => 'kb',
'mb' => 'mb',
'gb' => 'gb',
'and' => 'and',
'width' => 'width',
'height'=> 'height',
'files' => 'files',
'file' => 'file',
],
config: [
'limit' => 1,
'mime' => 'image', // video|audio|file|image|zip|pdf|xls|doc|general_image|general_media|general_file
'size' => '2mb',
'baseDir' => 'public',
'driver' => 'local',
'structure' => 'default', // default|year|month|day
'generate' => true, // will always generate a unique() name for each uploaded file
],
class: [
'error' => 'bg-danger',
'success' => 'bg-success',
]
);
- or -- using file class method
Global Config
(new File)->globalConfig(
message: [],
config: [],
class: []
);
<input type="file" name="avatar">
<input type="file" name="avatar[]" multiple>
- This will return error message
$file = File::name('html_input_name');
$file->getMessage();
// This will change the message text
$file->getMessage('new Message');
- This will return error status code
$file = File::name('html_input_name');
$file->getStatus();
- This will only return msg, if there's an error or success
$file = File::name('html_input_name');
$file->getClass();
- This will get the first uploaded data
- [optional] You can pass the mode as string
name
| path
|url
->save(function($response){
$response->first();
});
$upload = File::name('avatar')
->save();
$upload->first('url);
- This will get all uploaded data
- Returns an index array of all uploaded data,
[name, path, url]
- [optional] You can pass the mode as string
name
| path
|url
->save(function($response){
$response->get();
});
$upload = File::name('avatar')
->save();
$upload->get('name);
- Return form request data
- This will return
validator package
object Validator
->save(function($response){
$response->form();
});
- Accepts two param as string. This method uses the base path automatically.
- [mandatory]
$fileToUnlink
Path to file, you want to unlink.
- [optional]
$checkFile
. This will check if $fileToUnlink
is not same, before.
->save(function($response){
$userAvatar;
$response->unlink("public/images/{$userAvatar}", "avatar.svg");
<!-- the above will only unlink when value is not avatar.svg -->
});
- Takes one param
string
as input name
File::name('html_input_name');
- More drivers are to be added in the future
- By default driver is set to
local
Key |
Description |
s3 |
Amazon s3 driver. The package loaded [Ec2 and CloudWatch] needed by s3 |
local |
Local host server driver. |
File::name('avatar')
->driver('s3');
using driver if project is not in any Frameworks
use Tamedevelopers\Support\Env;
// if your project is not on on core php, then you'll need to load env.
// this will create env dummy data and as well load the .env file.
// once data has been created, you can remove the `Env::createOrIgnore();`
Env::createOrIgnore();
Env::load();
File::name('avatar')
->driver('s3');
- Takes one param
string
as base directory name
- This will override the global configuration settings (Domain and Server Path will be set)
File::name('avatar')
->baseDir('newBaseDirectory');
File::name('avatar')
->generate(false);
- Takes one param
string
as folder_path
to save file
File::name('avatar')
->folder('upload/user');
- Takes index or closed array index
- Remove
error status code
you do not want to validate
- You cannot remove Error
200
Status Code |
Description |
401 |
Select file to upload |
402 |
File upload is greater than allowed size of |
403 |
Maximum file upload exceeded. Limit is |
404 |
Uploaded file format not allowed. Allowed formats |
405 |
Image dimension allowed is |
200 |
File uploaded successfully |
File::name('avatar')
->filter(401, 402);
File::name('avatar')
->filter([401, 402, 405]);
- Takes one param
string
as structure type
- Best used for
Media\|Blog\|Newsletter
Websites.
Key |
Description |
default |
Files will be uploaded in defaut folder path |
year |
Files will be uploaded in default/year path: folder/yyyy |
month |
Files will be uploaded in default/year path: folder/yyyy/mm |
day |
Files will be uploaded in default/year path: folder/yyyy/mm/dd |
- Default
File::name('avatar')
->structure('month');
- Takes one param
string
| int
- size in
int
| kb
| mb
| gb
File::name('avatar')
->size('1.5mb'); // will be converted to: 1.5 * (1024 * 1024) = 1572864
File::name('avatar')
->size(2097152); // = 2097152|2mb
- Takes one param
string
| int
- Default limit is set to
1
upload
File::name('avatar')
->limit(2);
- Takes one param
string
as mime type
- Goto
Mime Types
to see list
File::name('avatar')
->mime('image');
- Takes two param
string
| int
and bool
- 1st param is
string
| int
. width size
- 2nd param is
bool
. This allow to check if size should be === or >= size of uploaded image. Default is true
$file = File::name('avatar')
->width(700, false);
dd(
$file
);
File::name('avatar')
->width(700)
->height(400);
- [optional] Method can be use, to return error message.
- Takes an [optional] param as a
callable\|closure
function.
File::name('banner')
->folder('upload/banner')
->validate(function($response){
// perform any other needed task in here
echo $response->getMessage();
return;
});
- Takes an [optional] param as a
callable\|closure
function.
- Calling this [method] will automatically save the data.
File::name('banner')
->folder('upload/banner')
->save(function($response){
// perform any other needed task in here
});
$file = File::name('banner')
->folder('upload/banner')
->save();
dd(
$file->get(),
$file->first(),
);
- Takes two param as
size
int
width and height
- Returns an instance of self
File::name()
->folder('upload/banner')
->save(function($response){
// perform resize
// width, height
$response->resize(400, 400);
});
- Takes three param
watermarkSource
| position
| padding
- [mandatory] $watermarkSource
- Returns an instance of self
- Padding is applied evenly on all position apart from
center
Position key |
Description |
bottom-right |
Default is bottom-right |
bottom-left |
apply watermart to possition bottom-left |
top-right |
apply watermart to possition top-right |
top-left |
apply watermart to possition top-left |
center |
apply watermart to possition center |
File::name('avatar')
->folder('upload/banner')
->save(function($response){
// perform watermark
$response->watermark('watermark.png', 'center', 50);
});
- Returns an instance of self
File::name('avatar')
->folder('upload/banner')
->save(function($response){
// perform compressor
$response->compress();
// you can perform method chaining as well
$response->resize(200, 450)
->watermark('watermark.png', 'center')
->compress();
});
- Takes one param as
string
File::getImageSize('full_source_path')
[
["height"] => int(4209)
["width"] => int(3368)
]
- Takes one param as
string
File::imageSize('name_of_uploaded_file')
[
["height"] => int(4209)
["width"] => int(3368)
]
- Takes one param as
string
- Return
string
| bool
. false
on error.
File::getMimeType('full_source_path')
- Takes one param as
string
. Input file name
File::notEmpty('avatar');
File::isNotEmpty('avatar');
File::has('avatar');
- Returns true or false. Check if there's an error in the upload
$file = File::name('avatar')
if($file->hasError()){
}
- Returns true or false. Check if upload has been completed
$file = File::name('avatar')
if($file->isCompleted()){
}
Key |
Description |
video |
.mp4|.mpeg|.mov|.avi|.wmv |
audio |
.mp3|.wav |
file |
.docx|.doc|.pdf|.txt |
image |
.jpg|.jpeg|.png|.gif |
zip |
.zip|.rar |
pdf |
.pdf |
xls |
.xlsx|.xls |
doc |
.docx|.doc|.txt |
general_image |
.jpg|.jpeg|.png|.gif|.webp|.ico |
general_media |
.mp3|.wav|.mp4|.mpeg|.mov|.avi|.wmv |
general_file |
.docx|.doc|.pdf|.txt|.zip|.rar|.xlsx|.xls |
File::name('invoiceDescription')
->mime('zip')
->save();