{"id":26698729,"url":"https://github.com/benhall14/php-upload","last_synced_at":"2025-08-07T17:37:00.416Z","repository":{"id":62493076,"uuid":"145346809","full_name":"benhall14/php-upload","owner":"benhall14","description":"A PHP class that makes handling uploads and file validation a lot simpler. You can use the chainable methods to set up the upload parameters such as min/max file size, mime types, and file extensions.","archived":false,"fork":false,"pushed_at":"2019-11-04T16:51:13.000Z","size":12,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-20T02:02:41.607Z","etag":null,"topics":["file-handling","file-upload","php","upload","uploader","uploading","uploads","validation"],"latest_commit_sha":null,"homepage":"https://conobe.co.uk/projects/php-upload","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/benhall14.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-20T00:12:41.000Z","updated_at":"2020-05-15T12:50:01.000Z","dependencies_parsed_at":"2022-11-02T11:17:30.693Z","dependency_job_id":null,"html_url":"https://github.com/benhall14/php-upload","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benhall14%2Fphp-upload","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benhall14%2Fphp-upload/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benhall14%2Fphp-upload/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benhall14%2Fphp-upload/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/benhall14","download_url":"https://codeload.github.com/benhall14/php-upload/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245743428,"owners_count":20665094,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["file-handling","file-upload","php","upload","uploader","uploading","uploads","validation"],"created_at":"2025-03-26T22:19:04.863Z","updated_at":"2025-03-26T22:19:05.626Z","avatar_url":"https://github.com/benhall14.png","language":"PHP","readme":"# PHP Upload\nA PHP class that makes handling uploads and file validation a lot simpler. You can use the chainable methods to set up the upload parameters such as min/max file size, mime types, and file extensions. Once set up, you can simply turn on the ***ignition().*** \n\nFor a more advanced upload option, this class allows you to add callbacks and loop through the files awaiting upload to perform your own validation and checks.\n\nTested to work with *PHP 5.6 and **PHP 7.***\n\n# Installation with composer\nYou can now install the upload class with composer:\n\t\n\t$ composer require benhall14/phpUpload\n\nPlease make sure that you include the composer autoloader and use the correct namespace.\n\n\trequire 'vendor/autoload.php';\n\n\tuse benhall14\\phpUpload\\Upload as Upload;\n\n\n# API Options\n\n## -\u003eversion()\n\nThe *version()* method will return the class version.\n\n    $upload-\u003eversion();\n\n## -\u003esubmitted()\n\nThe *submitted()* method will return either **true** or **false** depending on if the selected input element has been submitted or not.\n\n    $upload-\u003esubmitted();\n\n## -\u003esetMessages()\n\nYou can override the default messages by passing an array of custom messages to the *setMessages()* method.\n\n    $upload-\u003esetMessages($custom_messages);\n\nThe default messages are:\n    \n    $messages = [\n        # When the file element in the class is invalid.\n        'invalid_file_element' =\u003e 'Invalid File Element ID', \n        # When no files have been selected for upload.\n        'nothing_uploaded' =\u003e 'No files have been selected to upload.', \n        # When the destination path is invalid.\n        'invalid_destination' =\u003e 'Invalid Destination Path', \n        # When the upload path could not be created.\n        'could_not_create_path' =\u003e 'The %s doesn\\'t exist and could not be created.', \n        # When the file element is missing from the __construct() method.\n        'file_input_missing' =\u003e 'The file id %s is missing from the upload form.', \n        # When the uploaded file(s) don't fit the criteria for the minimum file size.\n        'too_small' =\u003e 'Too small', \n        # When the uploaded file(s) don't fit the criteria for the maximum file size.\n        'too_large' =\u003e 'Too large', \n        # When the uploaded file(s) don't fit the criteria for the allowed mime types.\n        'invalid_mime' =\u003e 'Invalid mime type uploaded (%s). Allowed mime types are: %s.', \n        # When the uploaded file(s) don't fit the criteria for the allowed file extensions.\n        'invalid_ext' =\u003e 'Invalid file type uploaded (%s). Allowed file types are: %s.', \n        # When the uploaded file(s) could not be moved due to a server or permission error.\n        'upload_move_error' =\u003e 'The file could not be uploaded: Permission Error', \n        # When the destination path set does not exist and/or could not be created.\n        'destination_missing' =\u003e 'The destination path configuration setting is missing.', \n    ];\n\n## -\u003egetAllowedExtensions()\n\nThis will return the current list of allowed file extensions.\n\n\t$extensions = $upload-\u003egetAllowedExtensions();\n\t\n## -\u003esetAllowedFileExtensions()\nThis is a basic, although insecure, method of matching the file extension uploaded with a preset list of allowable extensions. Allowed file types can be either an array of file extensions **array('jpg', 'png', 'gif')**, a pipe-separated list **'jpg|png|gif'**, or the **'*'** wild-card string.\n\n\t$upload-\u003esetAllowedFileExtensions('jpg|png|gif');\n\t# or\n\t$upload-\u003esetAllowedFileExtensions('*');\n\t# or\n\t$upload-\u003esetAllowedFileExtensions(array('jpg', 'png', 'gif'));\n\n## -\u003esetAllowedMimeTypes()\nThis is a more secure method of matching the file mime types uploaded with a preset list of allowable mime types. The allowed mime types can be either an array of mime types **array('image/jpg', 'image/png')** or the **'*'** wild-card string.\n\n\t# to allow all types\n\t$upload-\u003esetAllowedMimeTypes('*');\n\t\n\t# to only allow jpg and png images.\n\t$upload-\u003esetAllowedMimeTypes(array('image/jpg', 'image/png'));\n\n## -\u003egetAllowedMimeTypes()\n\nThis will return the current list of allowed mime types.\n\t\n\t$mime_types = $upload-\u003egetAllowedMimeTypes();\n\n## -\u003egetMaxFileSize()\nThis will return the current maximum set file size in bytes.\n\t\n\t$size = $upload-\u003egetMaxFileSize();\n\t\n## -\u003esetMaxFileSize($maximum_file_size = false, $type = 'b')\n\nThis will set the **MAXIMUM** size that is permitted for the upload. The first parameter is the integer size and the second parameter is the type - such as b, kb, mb or gb.\n\n\t$upload-\u003esetMaxFileSize(10, 'mb');\n\n## -\u003egetMinFileSize()\nThis will return the current minimum set file size in bytes.\n\t\n\t$size = $upload-\u003egetMaxFileSize();\n\n## -\u003esetMinFileSize($minimum_file_size = false, $type = 'b')\n\nThis will set the **MINIMUM** size that is permitted for the upload. The first parameter is the integer size and the second parameter is the type - such as b, kb, mb or gb.\n\n\t$upload-\u003esetMinFileSize(1, 'mb');\n\n## -\u003esetDestinationPath($path = false)\nSets the destination path for the uploads. It can either be a absolute path or a relative path to the script.\n\n\t$upload-\u003esetDestinationPath('uploads/');\n\n## -\u003esetExtensionAs($extension = null)\nForces the uploaded file to use the passed $extension. This doesn't change the mime type - just forces the new file extension. Useful if you only want to accept a certain file type - such as jpg, it allows the forcing of the jpg extension.\n\n\t$upload-\u003esetExtensionAs('jpg');\n\n## -\u003esetName($name = false, $clean = true)\nSets the overriding file name - using the cleanFilename method. **This can only be used with single element files**\n\n\t$upload-\u003esetName('my-image');\n\n## -\u003egenerateName($bool)\nSets the configuration option to create random filenames on the fly.\n\n\t$upload-\u003egenerateNames(true);\n\n## -\u003eignition()\nThis is the main ignition switch. This must be called **after** all of the configuration options have been set.\n\nThis will populate the file counts  \u0026 internal file objects. *This starts the upload engine.*\n\n\t$upload-\u003eignition();\n\n## -\u003ehasErrors()\nThis will return **true** or **false** depending on if an error has occurred. It can be combined with *errors()* to show a list of errors. \n\n\tif($upload-\u003ehasErrors()){\n\t\tforeach($upload-\u003eerrors() as $error){\n\t\t\techo '\u003cli\u003eERROR: ' . $error . '\u003c/li\u003e';\n\t\t}\n\t}\n\n## -\u003eerrors()\nThis returns an array of errors that occurred during the upload process. It can be combined with *hasError()*.\n\n\tif($upload-\u003ehasErrors()){\n\t\tforeach($upload-\u003eerrors() as $error){\n\t\t\techo '\u003cli\u003eERROR: ' . $error . '\u003c/li\u003e';\n\t\t}\n\t}\n\n## -\u003eisMultiple()\nThis will return **true** or **false** is the there are multiple files being uploaded.\n\n\t$is_multiple = $upload-\u003eisMultiple();\n\t\n## -\u003efileCount()\nThis will return the number of file uploads submitted.\n\n\t$count = $upload-\u003efileCount();\n\t\n## -\u003edebug()\nThis will print a debug message for development and testing.\n\n\t$upload-\u003edebug();\n\n## -\u003esuccesses()\nThis will return an array of successfully uploaded files.\n\n\tforeach($upload-\u003esuccesses() as $files){\n\t\techo $file-\u003ename . ' has been uploaded';\n\t}\n\n## -\u003efiles()\nThis will return an array of file objects. If called after the upload()  will also return the status of the upload.\n\n\tforeach($upload-\u003efiles() as $files){\n\t\techo $file-\u003ename;\n\t}\n\t\n## -\u003eupload()\nThis automatically performs the validation and processing on the uploaded files without additional coding.\n\n\t$upload-\u003eupload();\n\n# Callback Options\nThe reason for this class is to provide the easiest way to handle uploads and validation without having to manually perform all of the checks. The following callback methods can be used for advanced upload integration. If you are looking for a simple integration - see **upload()** above.\n\n## -\u003eeach($callback)\nThe **each()** accepts a callback and it loops through each file waiting to be uploaded.\n\n\t$upload-\u003eeach(function($file){\n\t\t# this will print the file object for each upload found.\n\t\tprint_r($file);\n\t});\n\n## -\u003esuccess($callback)\nApplies the callback to all of the successfully upload files.\n\t\n\t$upload-\u003esuccess(function($file){\n\t\techo $file-\u003ename . ' has been successfully uploaded.';\n\t});\n\n## -\u003eerror($callback)\nApplies the callback to all of the uploads that have had an error.\n\n\t$upload-\u003eerror(function($file){\n\t\techo $file-\u003ename . ' could not be uploaded due to an error.';\t\n\t});\n\t\n## -\u003evalidate($file)\nRuns the validation method on the $file supplied. This is for use within an **each()** callback.\n\n\t$upload-\u003eeach(function($file){\n\t\t$isValid = $this-\u003evalidate($file);\n\t});\n\n## -\u003eprocess($file)\nRuns the actual upload on the $file supplied. This is for use within an **each()** callback.\n\n\t$upload-\u003eeach(function($file){\n\t\t$isValid = $this-\u003evalidate($file);\n\t\tif($isValid){\n\t\t\t$this-\u003eprocess($file);\n\t\t}\n\t});\n\t\n#$file\nAll of the methods that return a **$file** object will have the following properties.\n\n\t$file-\u003eid; # The id of the upload.\n\t$file-\u003esource-\u003epath; # the source tmp_path\n\t$file-\u003esource-\u003efilename; # the source file name\n\t$file-\u003esource-\u003eextension; # the source file extension\n\t$file-\u003esource-\u003ename; # the source file name\n\t$file-\u003esource-\u003esize; # the source size\n\t$file-\u003esource-\u003etype; # the source type from $_FILES\n \t$file-\u003esource-\u003emime_type; # the source mime type\n\t$file-\u003esource-\u003eerror; # the $_FILES error.\n\t$file-\u003edestination-\u003esize; # the destination file size\n\t$file-\u003edestination-\u003emime_type; # the destination mime type\n \t$file-\u003edestination-\u003eextension; # the destination file extension\n\t$file-\u003edestination-\u003efilename; # the destination file nameof\n\t$file-\u003edestination-\u003epath; # the destination file path\n\t$file-\u003esuccess # returns boolean true/false\n\t$file-\u003eisValid # returns boolean true/false\n\n# Example: Simple Upload\nIn this example, we show the uploader in its simplest form. We are using the following HTML code:\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n\t\u003cbody\u003e\n\t\t\u003cform method=\"post\" enctype=\"multipart/form-data\"\u003e\n\t\t    Select image to upload:\n\t\t    \u003cinput type=\"file\" name=\"image_upload[]\" id=\"image_upload\"\u003e\n\t\t    \u003cinput type=\"file\" name=\"image_upload[]\" id=\"image_upload\"\u003e\n\t\t    \u003cinput type=\"file\" name=\"image_upload[]\" id=\"image_upload\"\u003e\n\t\t    \u003cinput type=\"file\" name=\"image_upload[]\" id=\"image_upload\"\u003e\n\t\t    \u003cinput type=\"submit\" value=\"Upload Image\" name=\"submit\"\u003e\n\t\t\u003c/form\u003e\n\t\u003c/body\u003e\n\u003c/html\u003e\n```\nPHP:\n\n\ttry {\n\t\t$upload = new Upload('image_upload');\n\n\t\tif ($upload-\u003esubmitted()) {\n\t\t\t$upload\n\t\t\t\t-\u003esetDestinationPath('uploads/') \n\t\t\t\t-\u003esetAllowedMimeTypes(array('image/jpg', 'image/jpeg', 'image/png', 'text/plain'))\n\t\t\t\t-\u003esetMaxFileSize(2, 'mb')\n\t\t\t\t-\u003eignition()\n\t\t\t\t-\u003eupload();\n\n\t\t\tif ($upload-\u003ehasErrors()) {\n\t\t\t\t# loop through each file that has error'd\n\t\t\t\tforeach ($upload-\u003eerrors() as $file) {\n\t\t\t\t\tforeach ($file-\u003eerrors as $error) {\n\t\t\t\t\t\techo '\u003cli\u003e' . $error . '\u003c/li\u003e';\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\techo 'Upload(s) Complete!';\n\t\t\t}\n\t\t}\n\t} catch (Exception $e) {\n\t\tdie($e-\u003egetMessage());\n\t}\n\n# Example: Advanced Upload\nIn this advanced example, we can showcase the callback methods. Again, we use the following HTML code:\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n\t\u003cbody\u003e\n\t\t\u003cform method=\"post\" enctype=\"multipart/form-data\"\u003e\n\t\t    Select image to upload:\n\t\t    \u003cinput type=\"file\" name=\"image_upload[]\" id=\"image_upload\"\u003e\n\t\t    \u003cinput type=\"file\" name=\"image_upload[]\" id=\"image_upload\"\u003e\n\t\t    \u003cinput type=\"file\" name=\"image_upload[]\" id=\"image_upload\"\u003e\n\t\t    \u003cinput type=\"file\" name=\"image_upload[]\" id=\"image_upload\"\u003e\n\t\t    \u003cinput type=\"submit\" value=\"Upload Image\" name=\"submit\"\u003e\n\t\t\u003c/form\u003e\n\t\u003c/body\u003e\n\u003c/html\u003e\n```\nPHP: \n\n\ttry {\n\t    $upload = new Upload('image_upload');\n\n\t    if ($upload-\u003esubmitted()) {\n\t\t\t$upload\n\t\t\t    -\u003esetDestinationPath('uploads/')\n\t\t\t    -\u003esetAllowedMimeTypes(array('image/jpg', 'image/jpeg', 'image/png', 'text/plain'))\n\t\t\t    -\u003esetMaxFileSize(2, 'mb')\n\t\t\t    -\u003eignition();\n\t\n\t\t\t$upload-\u003eeach(function ($file) use ($upload) {\n\t\t\t\tif ($upload-\u003evalidate($file)) {\n\n\t\t\t\t\t# we can use $file here and perform additional checks or manipulations.\n\n\t\t\t\t\t# now we process the upload\n\t\t\t\t\tif ($upload-\u003eprocess($file)) {\n\t\t\t\t\t    echo '\u003cli\u003e' . $file-\u003edestination-\u003ename . ' has been uploaded \u003cb\u003eSUCCESSFULLY\u003c/b\u003e.\u003c/li\u003e';\n\t\t\t\t\t    \n\t\t\t\t\t    return;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\techo '\u003cli\u003e\u003cb\u003eERROR:\u003c/b\u003e ' . $file-\u003esource-\u003ename . ' could not be uploaded.\u003c/li\u003e';\n\n\t\t\t\treturn;\n\t\t\t});\n\t\t}\n\t} catch (Exception $e) {\n\t\tdie($e-\u003egetMessage());\n\t}\n\n\n# Requirements\n**Tested to work with PHP 5.6 and PHP 7**\n\n# License\nCopyright (c) Benjamin Hall, ben@conobe.co.uk\nhttps://conobe.co.uk\n\nLicensed under the MIT license\n\n# Donate?\n\nIf you find this project helpful or useful in anyway, please consider getting me a cup of coffee - It's really appreciated :)\n\n[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://paypal.me/benhall14)","funding_links":["https://paypal.me/benhall14"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenhall14%2Fphp-upload","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbenhall14%2Fphp-upload","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenhall14%2Fphp-upload/lists"}