PHPackages                             joshmvc/filehandler - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. joshmvc/filehandler

ActiveLibrary[File &amp; Storage](/categories/file-storage)

joshmvc/filehandler
===================

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

1.0.3(7y ago)012MITPHP

Since May 22Pushed 7y agoCompare

[ Source](https://github.com/olayinkaokewale/FileHandler)[ Packagist](https://packagist.org/packages/joshmvc/filehandler)[ RSS](/packages/joshmvc-filehandler/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (4)DependenciesVersions (5)Used By (0)

PHP Library: FileHandler
========================

[](#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.

Installation
------------

[](#installation)

Recommended Method: Use [Composer](http://getcomposer.org/download/) 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](https://github.com/olayinkaokewale/FileHandler/archive/master.zip)

OR clone from github directly to your library directory.

Clone using SSH:

> :&lt;your\_username&gt;/FileHandler.git

Clone using HTTPS:

> &lt;your\_username&gt;/FileHandler.git

Usage Requirement(s)
--------------------

[](#usage-requirements)

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

```
require_once '/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)
-------------

[](#new-methods)

#### upload\_image($files, $destination, $file\_name="", $max\_size=0)

[](#upload_imagefiles-destination-file_name-max_size0)

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 2*1024*1024 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)

[](#upload_videofiles-destination-file_name-max_size0)

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 2*1024*1024 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)

[](#upload_audiofiles-destination-file_name-max_size0)

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 2*1024*1024 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)

[](#upload_documentfiles-destination-file_name-max_size0)

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 2*1024*1024 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)

[](#upload_compressedfiles-destination-file_name-max_size0)

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 2*1024*1024 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)
----------------

[](#legacy-methods)

#### \_\_upload($files, $destination, $constraints=\[\])

[](#__uploadfiles-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 ``
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
--------

[](#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

```

	Upload Files

```

> upload.php

```
