2017 © Pedro Peláez
 

library filehandler

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

image

joshmvc/filehandler

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

  • Friday, June 29, 2018
  • by olayinkaokewale
  • Repository
  • 0 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

PHP Library: FileHandler

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

Installation

Recommended Method: Use Composer to get the library by running the script below, (*2)

composer require joshmvc/filehandler

Other Installation Methods:, (*3)

Download zip and extract to your directory, (*4)

github download, (*5)

OR clone from github directly to your library directory., (*6)

Clone using SSH:, (*7)

git@github.com:/FileHandler.git, (*8)

Clone using HTTPS:, (*9)

https://github.com//FileHandler.git, (*10)

Usage Requirement(s)

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

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:, (*12)

require 'vendor/autoload.php';

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

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)., (*14)

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, (*15)

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)., (*16)

Example Usage:, (*17)

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)., (*18)

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

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)., (*20)

Example Usage:, (*21)

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)., (*22)

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

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)., (*24)

Example Usage:, (*25)

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., (*26)

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

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)., (*28)

Example Usage:, (*29)

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., (*30)

Accepted compressed file format include zip, rar, gz, z, gtar, tgz, (*31)

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)., (*32)

Example Usage:, (*33)

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., (*34)

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

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", (*36)

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...., (*37)

EXAMPLE:

```php
$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, (*38)

files_upload.html, (*39)

<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, (*40)

<?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., (*41)

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., (*42)

; 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., (*43)

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

The Versions

29/06 2018

dev-master

9999999-dev

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

  Sources   Download

MIT

by Olayinka Okewale

29/06 2018

1.0.3

1.0.3.0

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

  Sources   Download

MIT

by Olayinka Okewale

23/05 2018

1.0.2

1.0.2.0

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

  Sources   Download

MIT

by Olayinka Okewale

23/05 2018

1.0.1

1.0.1.0

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

  Sources   Download

MIT

by Olayinka Okewale

22/05 2018

1.0.0

1.0.0.0

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

  Sources   Download

MIT

by Olayinka Okewale