├── .github └── workflows │ └── ci.yml ├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── examples ├── aspect_ratios.php ├── base64.php ├── basic.php ├── custom_errors.php ├── data_url.php ├── dimensions.php ├── images.php ├── inline.php ├── multiple_files.php └── raw_input.php ├── phpunit.xml ├── src └── Uploader.php └── tests ├── CustomErrorsTest.php ├── FileExtensionTest.php ├── FileMimeTypeTest.php ├── FileNameTest.php ├── FilePathTest.php ├── FileSizeTest.php ├── ImageAspectRatiosTest.php ├── ImageDimensionsTest.php ├── MethodsTest.php ├── MustBeImageTest.php ├── UploadTest.php └── assets ├── foo.base64 ├── foo.jpg ├── foo.json ├── foo.png ├── ratio_16_9.png ├── ratio_3_2.png ├── ratio_4_3.png └── ratio_9_16.png /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: Testing Uploader 2 | on: [push, pull_request] 3 | jobs: 4 | tests: 5 | strategy: 6 | matrix: 7 | php-versions: ['7.2', '7.4', '8.0'] 8 | runs-on: ubuntu-latest 9 | steps: 10 | - name: Checkout 11 | uses: actions/checkout@v2 12 | - name: Setup PHP 13 | uses: shivammathur/setup-php@v2 14 | with: 15 | php-version: ${{ matrix.php-versions }} 16 | extensions: exif, fileinfo 17 | - name: Get composer cache directory 18 | id: composer-cache 19 | run: echo "::set-output name=dir::$(composer config cache-files-dir)" 20 | - name: Cache composer dependencies 21 | uses: actions/cache@v2 22 | with: 23 | path: ${{ steps.composer-cache.outputs.dir }} 24 | key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} 25 | restore-keys: ${{ runner.os }}-composer- 26 | - name: Install dependencies 27 | run: | 28 | composer install --no-progress --prefer-dist --optimize-autoloader 29 | - name: Test with phpunit 30 | run: vendor/bin/phpunit --coverage-text -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.idea 3 | /vendor 4 | /composer.lock 5 | /examples/upload 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Uploader 🚀 2 | Safe, simple and useful file upload class for PHP 5.4+ 3 | 4 | ### Installing 5 | ``` 6 | composer require iamdual/uploader "^0.2.0" 7 | ``` 8 | 9 | ### Examples 10 | Basic: 11 | ```php 12 | use iamdual\Uploader; 13 | 14 | if (isset($_FILES["file"])) { 15 | 16 | $upload = new Uploader($_FILES["file"]); 17 | $upload->allowed_extensions(array("png", "jpg", "jpeg", "gif")); 18 | $upload->max_size(5); // in MB 19 | $upload->path("upload/files"); 20 | $upload->name("foo"); 21 | 22 | if (! $upload->upload()) { 23 | echo "Upload error: " . $upload->get_error(); 24 | } else { 25 | echo "Upload successful!"; 26 | } 27 | } 28 | ``` 29 | 30 | Inline using: 31 | ```php 32 | use iamdual\Uploader; 33 | 34 | if (isset($_FILES["file"])) { 35 | $upload = (new Uploader($_FILES["file"]))->max_size(20)->path("upload/files")->encrypt_name(); 36 | 37 | if (! $upload->upload()) { 38 | echo "Upload error: " . $upload->get_error(); 39 | } else { 40 | echo "Upload successful!"; 41 | } 42 | } 43 | ``` 44 | 45 | More examples in the "[examples](/examples)" directory. 46 | 47 | ### Methods 48 | | Name | Description | 49 | |--------------------------------------------|--------------------------------------------------------------| 50 | | `allowed_extensions(array $extensions)` | Allowed file extensions (example: png, gif, jpg) | 51 | | `disallowed_extensions(array $extensions)` | Disallowed file extensions (example: html, php, dmg) | 52 | | `allowed_types(array $types)` | Allowed mime types (example: image/png, image/jpeg) | 53 | | `disallowed_types(array $types)` | Disallowed mime types | 54 | | `max_size(int $size)` | Maximum file size (as MB) | 55 | | `min_size(int $size)` | Minimum file size (as MB) | 56 | | `override()` | Override the file with the same name | 57 | | `path(string $path)` | Set the path where files will be uploaded | 58 | | `name(string $name)` | Rename the uploaded file (example: foo) | 59 | | `encrypt_name()` | Encrypt file name to hide the original name | 60 | | `must_be_image()` | Check the file is image | 61 | | `max_dimensions(int $width, int $height)` | Maximum image dimensions | 62 | | `min_dimensions(int $width, int $height)` | Minimum image dimensions | 63 | | `aspect_ratios(array $aspect_ratios)` | Image aspect ratios that has to be (example: 1:1, 4:3, 16:9) | 64 | | `error_messages(array $errors)` | Custom error messages | 65 | 66 | | Name | Description | Return | 67 | |------------------|--------------------------------------------------|---------| 68 | | `upload()` | Upload the file and return output of the check() | boolean | 69 | | `check()` | Check the file can be uploaded | boolean | 70 | | `get_name()` | Get the uploaded file name | string | 71 | | `get_path()` | Get the uploaded file name with full path | string | 72 | | `get_tmp_name()` | Get the temporary file path | string | 73 | | `get_size()` | Get the uploaded file size in bytes | string | 74 | | `get_type()` | Get the uploaded file mime type | string | 75 | | `get_data_url()` | Get the file as base64 encoded data URL | string | 76 | | `get_error()` | Get error message if an error occurred | string | 77 | 78 | ### Notes 79 | `exif` and `fileinfo` extensions must be enabled. 80 | 81 | ### Contributes 82 | Please send pull request or open an issue if you have the feature you want. 83 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "iamdual/uploader", 3 | "description": "Safe, simple and useful file upload class for PHP.", 4 | "homepage": "https://github.com/iamdual/uploader", 5 | "type": "library", 6 | "license": "Apache-2.0", 7 | "authors": [ 8 | { 9 | "name": "Ekin Karadeniz", 10 | "email": "iamdual@protonmail.com" 11 | } 12 | ], 13 | "require": { 14 | "php": ">=5.4.0" 15 | }, 16 | "autoload": { 17 | "psr-4": { 18 | "iamdual\\": "src/" 19 | } 20 | }, 21 | "require-dev": { 22 | "phpunit/phpunit": "^8" 23 | }, 24 | "scripts": { 25 | "test": "phpunit" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /examples/aspect_ratios.php: -------------------------------------------------------------------------------- 1 | must_be_image(); 10 | $upload->aspect_ratios(array("16:9", "1:1")); 11 | $upload->max_size(5); // in MB 12 | $upload->path("upload/files"); 13 | 14 | if (!$upload->upload()) { 15 | echo "Upload error: " . $upload->get_error(); 16 | } else { 17 | echo "Upload successful!"; 18 | } 19 | 20 | } 21 | ?> 22 | 23 |
24 | Select a file with aspect ratio 16:9 or 1:1 25 |
26 | -------------------------------------------------------------------------------- /examples/base64.php: -------------------------------------------------------------------------------- 1 | allowed_extensions(array("png", "jpg", "jpeg", "gif")); 10 | $upload->max_size(5); // in MB 11 | $upload->path("upload/files"); 12 | $upload->name("unicorn"); 13 | 14 | // While uploading file from base64 encoded file, you need to enable the "copy file" feature 15 | // by appending "true" to the upload() method. Otherwise file can not be uploaded! 16 | 17 | if (! $upload->upload(true)) { 18 | echo "Upload error: " . $upload->get_error(); 19 | } else { 20 | echo "Upload successful: " . $upload->get_name(); 21 | } 22 | 23 | } 24 | ?> 25 | 26 |
27 | Enter Base64 encoded file: 28 |
29 | -------------------------------------------------------------------------------- /examples/basic.php: -------------------------------------------------------------------------------- 1 | allowed_extensions(array("png", "jpg", "jpeg", "gif")); 10 | $upload->allowed_types(array("image/png", "image/jpeg", "image/gif")); // not recommended 11 | $upload->max_size(5); // in MB 12 | $upload->min_size(0); // in MB 13 | $upload->path("upload/files"); 14 | $upload->encrypt_name(); 15 | 16 | if (!$upload->upload()) { 17 | echo "Upload error: " . $upload->get_error(); 18 | } else { 19 | echo "Upload successful: " . $upload->get_name(); 20 | } 21 | 22 | } 23 | ?> 24 | 25 |
26 | Select File: 27 |
28 | -------------------------------------------------------------------------------- /examples/custom_errors.php: -------------------------------------------------------------------------------- 1 | error_messages(array( 10 | $upload::ERR_LONG_SIZE => "Fil3 siz3 is t00 l0ng!", 11 | $upload::ERR_INVALID_EXT => "Inv4lid 3xt3nsi0n!", 12 | )); 13 | $upload->max_size(0.2); // in MB 14 | $upload->allowed_extensions(array("baz")); 15 | $upload->path("upload/files"); 16 | 17 | if (!$upload->upload()) { 18 | echo "Upload error: " . $upload->get_error(); 19 | } else { 20 | echo "Upload successful!"; 21 | } 22 | 23 | } 24 | ?> 25 | 26 |
27 | Select File: 28 |
29 | -------------------------------------------------------------------------------- /examples/data_url.php: -------------------------------------------------------------------------------- 1 | allowed_extensions(array("png", "jpg", "jpeg", "gif")); 10 | $upload->max_size(5); // in MB 11 | 12 | if (!$upload->check()) { 13 | echo "An error occurred: " . $upload->get_error(); 14 | } else { 15 | echo 'Base64 encoded data URL:
'; 16 | } 17 | 18 | } 19 | ?> 20 | 21 |
22 | Select File: 23 |
24 | -------------------------------------------------------------------------------- /examples/dimensions.php: -------------------------------------------------------------------------------- 1 | must_be_image(); 10 | $upload->min_dimensions(1024, null); // minimum 1024px width & **any** height is allowed 11 | $upload->max_dimensions(2048, 1000); // maximum 2048px width & 1000px height is allowed 12 | $upload->max_size(5); // in MB 13 | $upload->path("upload/files"); 14 | 15 | if (!$upload->upload()) { 16 | echo "Upload error: " . $upload->get_error(); 17 | } else { 18 | echo "Upload successful!"; 19 | } 20 | 21 | } 22 | ?> 23 | 24 |
25 | Select File: 26 |
27 | -------------------------------------------------------------------------------- /examples/images.php: -------------------------------------------------------------------------------- 1 | must_be_image(); 10 | $upload->max_size(5); // in MB 11 | $upload->path("upload/files"); 12 | 13 | if (!$upload->upload()) { 14 | echo "Upload error: " . $upload->get_error(); 15 | } else { 16 | echo "Upload successful!"; 17 | } 18 | 19 | } 20 | ?> 21 | 22 |
23 | Select File: 24 |
25 | -------------------------------------------------------------------------------- /examples/inline.php: -------------------------------------------------------------------------------- 1 | max_size(20)->path("upload/files")->encrypt_name(); 9 | 10 | if (!$upload->upload()) { 11 | echo "Upload error: " . $upload->get_error(); 12 | } else { 13 | echo "Upload successful!"; 14 | } 15 | 16 | } 17 | ?> 18 | 19 |
20 | Select File: 21 |
22 | -------------------------------------------------------------------------------- /examples/multiple_files.php: -------------------------------------------------------------------------------- 1 | allowed_extensions(array("png", "jpg", "jpeg", "gif")); 13 | $upload->max_size(5); // in MB 14 | $upload->path("upload/files"); 15 | $upload->encrypt_name(); 16 | 17 | if (!$upload->upload()) { 18 | echo $upload->get_name() . ": Upload error: " . $upload->get_error() . "
"; 19 | } else { 20 | echo $upload->get_name() . ": Upload successful!
"; 21 | } 22 | } 23 | 24 | } 25 | ?> 26 | 27 |
28 | Select Multiple Files: 29 |
30 | -------------------------------------------------------------------------------- /examples/raw_input.php: -------------------------------------------------------------------------------- 1 | max_size(5); // in MB 10 | $upload->path("upload/files"); 11 | 12 | // While uploading file from raw input data, you need to change upload function 13 | // to "copy", otherwise file can not be uploaded! 14 | 15 | if (! $upload->upload(true)) { 16 | echo "Upload error: " . $upload->get_error() . PHP_EOL; 17 | } else { 18 | echo "Upload successful: " . $upload->get_name() . PHP_EOL; 19 | } 20 | 21 | } else { 22 | 23 | echo << 27 |
28 | curl --upload-file ./hello.txt http://upload.local/examples/raw_input.php
29 | 
30 | 31 | HTML; 32 | 33 | } 34 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | tests 14 | 15 | 16 | 17 | 18 | src 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/Uploader.php: -------------------------------------------------------------------------------- 1 | 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | namespace iamdual; 19 | 20 | class Uploader 21 | { 22 | const ERR_EMPTY_FILE = 1; 23 | const ERR_INVALID_EXT = 2; 24 | const ERR_INVALID_TYPE = 3; 25 | const ERR_LONG_SIZE = 4; 26 | const ERR_SMALL_SIZE = 5; 27 | const ERR_UNKNOWN_ERROR = 6; 28 | const ERR_NOT_AN_IMAGE = 7; 29 | const ERR_MAX_DIMENSION = 8; 30 | const ERR_MIN_DIMENSION = 9; 31 | const ERR_ASPECT_RATIO = 10; 32 | 33 | /** 34 | * Error ID 35 | * @var int 36 | */ 37 | private $error = null; 38 | 39 | /** 40 | * The file array 41 | * @var array 42 | */ 43 | private $file = null; 44 | 45 | /** 46 | * Default error messages 47 | * @var array 48 | */ 49 | private $error_messages = array( 50 | self::ERR_EMPTY_FILE => "No file selected.", 51 | self::ERR_INVALID_EXT => "Invalid file extension.", 52 | self::ERR_INVALID_TYPE => "Invalid file mime type.", 53 | self::ERR_LONG_SIZE => "File size is too large.", 54 | self::ERR_SMALL_SIZE => "File size is too small.", 55 | self::ERR_UNKNOWN_ERROR => "Unknown error occurred.", 56 | self::ERR_NOT_AN_IMAGE => "The selected file must be an image.", 57 | self::ERR_MAX_DIMENSION => "The dimensions of the image is too large.", 58 | self::ERR_MIN_DIMENSION => "The dimensions of the image is too small.", 59 | self::ERR_ASPECT_RATIO => "The aspect ratio of the image is not as specified.", 60 | ); 61 | 62 | /** 63 | * Customized error messages 64 | * @var array 65 | */ 66 | public $custom_error_messages = null; 67 | 68 | /** 69 | * @var array 70 | */ 71 | public $extensions = null; 72 | 73 | /** 74 | * @var array 75 | */ 76 | public $disallowed_extensions = null; 77 | 78 | /** 79 | * @var array 80 | */ 81 | public $types = null; 82 | 83 | /** 84 | * @var array 85 | */ 86 | public $disallowed_types = null; 87 | 88 | /** 89 | * @var int 90 | */ 91 | public $max_size = null; 92 | 93 | /** 94 | * @var int 95 | */ 96 | public $min_size = null; 97 | 98 | /** 99 | * @var string 100 | */ 101 | public $path = null; 102 | 103 | /** 104 | * @var string 105 | */ 106 | public $name = null; 107 | 108 | /** 109 | * @var boolean 110 | */ 111 | public $auto_extension = true; 112 | 113 | /** 114 | * @var boolean 115 | */ 116 | public $must_be_image = false; 117 | 118 | /** 119 | * @var array 120 | */ 121 | public $max_image_dimensions = null; 122 | 123 | /** 124 | * @var array 125 | */ 126 | public $min_image_dimensions = null; 127 | 128 | /** 129 | * @var array 130 | */ 131 | public $image_aspect_ratios = null; 132 | 133 | /** 134 | * @var boolean 135 | */ 136 | public $encrypt_name = false; 137 | 138 | /** 139 | * @var boolean 140 | */ 141 | public $override = false; 142 | 143 | /** 144 | * Set the file array ($_FILES or equivalent of this) 145 | * @param array $file 146 | */ 147 | function __construct($file) 148 | { 149 | $this->file = $file; 150 | return $this; 151 | } 152 | 153 | /** 154 | * Allowed file extensions (example: png, gif, jpg) 155 | * @param array $extensions 156 | * @return $this 157 | */ 158 | public function allowed_extensions($extensions) 159 | { 160 | $this->extensions = (is_array($extensions)) ? $extensions : null; 161 | return $this; 162 | } 163 | 164 | /** 165 | * Disallowed file extensions (example: html, php, dmg) 166 | * @param array $extensions 167 | * @return $this 168 | */ 169 | public function disallowed_extensions($extensions) 170 | { 171 | $this->disallowed_extensions = (is_array($extensions)) ? $extensions : null; 172 | return $this; 173 | } 174 | 175 | /** 176 | * Allowed mime types (example: image/png, image/jpeg) 177 | * @param array $types 178 | * @return $this 179 | */ 180 | public function allowed_types($types) 181 | { 182 | $this->types = (is_array($types)) ? $types : null; 183 | return $this; 184 | } 185 | 186 | /** 187 | * Disallowed mime types 188 | * @param array $types 189 | * @return $this 190 | */ 191 | public function disallowed_types($types) 192 | { 193 | $this->disallowed_types = (is_array($types)) ? $types : null; 194 | return $this; 195 | } 196 | 197 | /** 198 | * Maximum file size in MB 199 | * @param int $size 200 | * @return $this 201 | */ 202 | public function max_size($size) 203 | { 204 | $this->max_size = (is_numeric($size)) ? $size : null; 205 | return $this; 206 | } 207 | 208 | /** 209 | * Minimum file size in MB 210 | * @param int $size 211 | * @return $this 212 | */ 213 | public function min_size($size) 214 | { 215 | $this->min_size = (is_numeric($size)) ? $size : null; 216 | return $this; 217 | } 218 | 219 | /** 220 | * Maximum dimensions of the image 221 | * @param int $width 222 | * @param int $height 223 | * @return $this 224 | */ 225 | public function max_dimensions($width, $height) 226 | { 227 | $this->max_image_dimensions = array($width, $height); 228 | return $this; 229 | } 230 | 231 | /** 232 | * Minimum dimensions of the image 233 | * @param int $width 234 | * @param int $height 235 | * @return $this 236 | */ 237 | public function min_dimensions($width, $height) 238 | { 239 | $this->min_image_dimensions = array($width, $height); 240 | return $this; 241 | } 242 | 243 | /** 244 | * @deprecated 245 | * DEPRECATED: Use max_dimensions() 246 | */ 247 | public function max_image_dimensions($width, $height) 248 | { 249 | return $this->max_dimensions($width, $height); 250 | } 251 | 252 | /** 253 | * @deprecated 254 | * DEPRECATED: Use min_dimensions() 255 | */ 256 | public function min_image_dimensions($width, $height) 257 | { 258 | return $this->min_dimensions($width, $height); 259 | } 260 | 261 | /** 262 | * Image aspect ratios has to be 263 | * @param array $aspect_ratios 264 | * @return $this 265 | */ 266 | public function aspect_ratios($aspect_ratios) 267 | { 268 | $this->image_aspect_ratios = $aspect_ratios; 269 | return $this; 270 | } 271 | 272 | /** 273 | * Override (write over) the file with the same name 274 | * @return $this 275 | */ 276 | public function override() 277 | { 278 | $this->override = true; 279 | return $this; 280 | } 281 | 282 | /** 283 | * The path where files will be uploaded 284 | * @param string $path 285 | * @return $this 286 | */ 287 | public function path($path) 288 | { 289 | $this->path = rtrim($path, "/"); 290 | return $this; 291 | } 292 | 293 | /** 294 | * Rename the uploaded file (example: foo) 295 | * @param string $name 296 | * @param boolean $auto_extension 297 | * @return $this 298 | */ 299 | public function name($name, $auto_extension = true) 300 | { 301 | $this->name = $name; 302 | $this->auto_extension = $auto_extension; 303 | return $this; 304 | } 305 | 306 | /** 307 | * Encrypt file name to hide the original name 308 | * @return $this 309 | */ 310 | public function encrypt_name() 311 | { 312 | $this->encrypt_name = true; 313 | return $this; 314 | } 315 | 316 | /** 317 | * Verify that the file is an image 318 | * @return $this 319 | */ 320 | public function must_be_image() 321 | { 322 | $this->must_be_image = true; 323 | return $this; 324 | } 325 | 326 | /** 327 | * Set custom error messages 328 | * @param array $errors 329 | * @return $this 330 | */ 331 | public function error_messages($errors) 332 | { 333 | $this->custom_error_messages = (is_array($errors)) ? $errors : null; 334 | return $this; 335 | } 336 | 337 | /** 338 | * Get error message 339 | * @param string $error_id 340 | * @return string 341 | */ 342 | public function get_error_message($error_id) 343 | { 344 | if ($this->custom_error_messages !== null && isset($this->custom_error_messages[$error_id])) { 345 | return $this->custom_error_messages[$error_id]; 346 | } 347 | return isset($this->error_messages[$error_id]) ? $this->error_messages[$error_id] : null; 348 | } 349 | 350 | /** 351 | * Get file name 352 | * @return string 353 | */ 354 | public function get_name() 355 | { 356 | if ($this->name === null) { 357 | $this->name = $this->file["name"]; 358 | $this->auto_extension = false; 359 | } 360 | 361 | if ($this->encrypt_name) { 362 | $this->name = self::hashed($this->name) . self::get_ext($this->file["name"], true); 363 | $this->encrypt_name = false; 364 | $this->auto_extension = false; 365 | } 366 | 367 | if ($this->auto_extension) { 368 | return $this->name . self::get_ext($this->file["name"], true); 369 | } else { 370 | return $this->name; 371 | } 372 | } 373 | 374 | /** 375 | * Get the name of the temporary file 376 | * @return string 377 | */ 378 | public function get_tmp_name() 379 | { 380 | return isset($this->file["tmp_name"]) ? $this->file["tmp_name"] : null; 381 | } 382 | 383 | /** 384 | * Get the mime type of the file 385 | * @return string 386 | */ 387 | public function get_type() 388 | { 389 | return isset($this->file["type"]) ? $this->file["type"] : null; 390 | } 391 | 392 | /** 393 | * Get the size of the file 394 | * @return int 395 | */ 396 | public function get_size() 397 | { 398 | return isset($this->file["size"]) ? $this->file["size"] : null; 399 | } 400 | 401 | /** 402 | * Get the data URL of the temporary file 403 | * @return string 404 | */ 405 | public function get_data_url() 406 | { 407 | return self::data_url($this->get_tmp_name()); 408 | } 409 | 410 | /** 411 | * Check the file can be uploaded 412 | * @return boolean 413 | */ 414 | public function check() 415 | { 416 | if (!is_array($this->file) || $this->error !== null) { 417 | return false; 418 | } 419 | 420 | // Standard validations 421 | if (!isset($this->file["name"]) || !isset($this->file["tmp_name"]) || !isset($this->file["type"]) || !isset($this->file["size"]) || !isset($this->file["error"])) { 422 | $this->error = self::ERR_EMPTY_FILE; 423 | } else if (strlen($this->file["name"]) == 0 || strlen($this->file["tmp_name"]) == 0 || strlen($this->file["type"]) == 0 || $this->file["size"] == 0) { 424 | $this->error = self::ERR_EMPTY_FILE; 425 | } else if ($this->extensions !== null && !in_array(self::get_ext($this->file["name"]), $this->extensions)) { 426 | $this->error = self::ERR_INVALID_EXT; 427 | } else if ($this->disallowed_extensions !== null && in_array(self::get_ext($this->file["name"]), $this->disallowed_extensions)) { 428 | $this->error = self::ERR_INVALID_EXT; 429 | } else if ($this->types !== null && !in_array($this->file["type"], $this->types)) { 430 | $this->error = self::ERR_INVALID_TYPE; 431 | } else if ($this->disallowed_types !== null && in_array($this->file["type"], $this->disallowed_types)) { 432 | $this->error = self::ERR_INVALID_TYPE; 433 | } else if ($this->max_size !== null && $this->file["size"] > self::mb_to_byte($this->max_size)) { 434 | $this->error = self::ERR_LONG_SIZE; 435 | } else if ($this->min_size !== null && $this->file["size"] < self::mb_to_byte($this->min_size)) { 436 | $this->error = self::ERR_SMALL_SIZE; 437 | } else if ($this->file["error"] == 1 || $this->file["error"] == 2) { 438 | $this->error = self::ERR_LONG_SIZE; 439 | } else if ($this->file["error"] == 4) { 440 | $this->error = self::ERR_EMPTY_FILE; 441 | } else if ($this->file["error"] > 0) { 442 | $this->error = self::ERR_UNKNOWN_ERROR; 443 | } 444 | 445 | if ($this->error !== null) { 446 | return false; 447 | } 448 | 449 | // Image validations 450 | if ($this->max_image_dimensions !== null || $this->min_image_dimensions !== null || $this->image_aspect_ratios) { 451 | $image_dimensions = getimagesize($this->file["tmp_name"]); 452 | if (!$image_dimensions) { 453 | $this->error = self::ERR_NOT_AN_IMAGE; 454 | return false; 455 | } 456 | if ($this->max_image_dimensions !== null) { 457 | for ($i = 0; $i <= 1; $i++) { 458 | if (isset($this->max_image_dimensions[$i]) && is_numeric($this->max_image_dimensions[$i]) && $image_dimensions[$i] > $this->max_image_dimensions[$i]) { 459 | $this->error = self::ERR_MAX_DIMENSION; 460 | return false; 461 | } 462 | } 463 | } 464 | if ($this->min_image_dimensions !== null) { 465 | for ($i = 0; $i <= 1; $i++) { 466 | if (isset($this->min_image_dimensions[$i]) && is_numeric($this->min_image_dimensions[$i]) && $image_dimensions[$i] < $this->min_image_dimensions[$i]) { 467 | $this->error = self::ERR_MIN_DIMENSION; 468 | return false; 469 | } 470 | } 471 | } 472 | if ($this->image_aspect_ratios !== null) { 473 | foreach ($this->image_aspect_ratios as $aspect_ratio) { 474 | if (self::validate_aspect_ratio($aspect_ratio, $image_dimensions[0], $image_dimensions[1])) { 475 | if ($this->error === self::ERR_ASPECT_RATIO) { 476 | $this->error = null; 477 | } 478 | break; // Validation completed. 479 | } else { 480 | $this->error = self::ERR_ASPECT_RATIO; 481 | } 482 | } 483 | } 484 | } else if ($this->must_be_image) { 485 | // If the file must be an image and getimagesize() didn't check the file, we need to use exif_imagetype instead of getimagesize for the performance. 486 | if (!exif_imagetype($this->file["tmp_name"])) { 487 | $this->error = self::ERR_NOT_AN_IMAGE; 488 | return false; 489 | } 490 | } 491 | 492 | return $this->error === null; 493 | } 494 | 495 | /** 496 | * Get error if exists 497 | * @param boolean $with_message (optional) 498 | * @return string 499 | */ 500 | public function get_error($with_message = true) 501 | { 502 | return $with_message ? $this->get_error_message($this->error) : $this->error; 503 | } 504 | 505 | /** 506 | * Upload the file. 507 | * @param boolean $copy_file (optional) 508 | * @return boolean 509 | */ 510 | public function upload($copy_file = false) 511 | { 512 | if ($this->check()) { 513 | $upload_dir = $this->get_path(null, false); 514 | if (!is_dir($upload_dir)) { 515 | @mkdir($upload_dir, 0777, true); 516 | } 517 | $filepath = $this->get_path(); 518 | if ($this->override === false && file_exists($filepath)) { 519 | $number = 2; 520 | $filename = pathinfo($filepath, PATHINFO_FILENAME); 521 | do { 522 | $this->name($filename . (($number) ? "_{$number}" : ""), true); 523 | $number++; 524 | } while (file_exists($this->get_path())); 525 | } 526 | $upload_function = $copy_file ? "copy" : "move_uploaded_file"; 527 | $upload_function($this->file["tmp_name"], $this->get_path()); 528 | return true; 529 | } else { 530 | return false; 531 | } 532 | } 533 | 534 | /** 535 | * Get the full path 536 | * @param string $filename (optional) 537 | * @param bool $include_filename (optional) 538 | * @return string 539 | */ 540 | public function get_path($filename = null, $include_filename = true) 541 | { 542 | $path = ""; 543 | if ($this->path !== null) { 544 | $path = $this->path . "/"; 545 | } 546 | if (!$include_filename) { 547 | return $path; 548 | } 549 | 550 | if ($filename === null) { 551 | $filename = $this->get_name(); 552 | } 553 | return $path . $filename; 554 | } 555 | 556 | /** 557 | * Get extension by filename 558 | * @param string $filename 559 | * @param boolean $with_dot 560 | * @return string 561 | */ 562 | public static function get_ext($filename, $with_dot = false) 563 | { 564 | $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); 565 | if ($with_dot && $extension) { 566 | return "." . $extension; 567 | } 568 | return $extension; 569 | } 570 | 571 | /** 572 | * Validate aspect ratio 573 | * @param mixed $aspect_ratio 574 | * @param int $width 575 | * @param int $height 576 | * @return bool 577 | */ 578 | public static function validate_aspect_ratio($aspect_ratio, $width, $height) 579 | { 580 | if (!is_numeric($aspect_ratio)) { 581 | if (is_string($aspect_ratio)) { 582 | $aspect_ratio_pieces = explode(":", $aspect_ratio); 583 | } else if (is_array($aspect_ratio)) { 584 | $aspect_ratio_pieces = $aspect_ratio; 585 | } 586 | if (empty($aspect_ratio_pieces[0]) || empty($aspect_ratio_pieces[1])) { 587 | return false; 588 | } 589 | $aspect_ratio = (int)$aspect_ratio_pieces[0] / (int)$aspect_ratio_pieces[1]; 590 | } 591 | 592 | return ($width / $height) === $aspect_ratio; 593 | } 594 | 595 | /** 596 | * Calculate the bytes 597 | * @param int $filesize 598 | * @return int 599 | */ 600 | public static function mb_to_byte($filesize) 601 | { 602 | return $filesize * 1048576; // equivalent of "pow(1024, 2)" 603 | } 604 | 605 | /** 606 | * Create multiple file array 607 | * @param array $file_array 608 | * @return array 609 | */ 610 | public static function multiple_file_array($file_array) 611 | { 612 | $files = array(); 613 | foreach ($file_array as $files_key => $files_array) { 614 | foreach ($files_array as $i => $val) { 615 | $files[$i][$files_key] = $val; 616 | } 617 | } 618 | return $files; 619 | } 620 | 621 | /** 622 | * Create file array from base64 encoded file 623 | * @param string $base64 624 | * @param array $mime_map (optional) 625 | * @return array 626 | */ 627 | public static function from_base64($base64, $mime_map = []) 628 | { 629 | $encoded = explode(";base64,", $base64, 2); 630 | if (isset($encoded[1])) { 631 | $base64 = $encoded[1]; 632 | } 633 | return self::create_temp_file(base64_decode($base64), $mime_map); 634 | } 635 | 636 | /** 637 | * Create file array from raw input 638 | * @param array $mime_map (optional) 639 | * @return array 640 | */ 641 | public static function from_raw_input($mime_map = []) 642 | { 643 | return self::create_temp_file(file_get_contents("php://input"), $mime_map); 644 | } 645 | 646 | /** 647 | * Create a temporary file from source 648 | * @param string $source 649 | * @param array $mime_map (optional) 650 | * @return array 651 | */ 652 | public static function create_temp_file($source, $mime_map = []) 653 | { 654 | $temp = tmpfile(); 655 | fwrite($temp, $source); 656 | $meta = stream_get_meta_data($temp); 657 | $mime = mime_content_type($meta["uri"]); 658 | 659 | if (isset($mime_map[$mime])) { 660 | $ext = $mime_map[$mime]; 661 | } else { 662 | $arr = explode("/", $mime); 663 | $ext = end($arr); 664 | } 665 | 666 | register_shutdown_function(function () use ($temp) { 667 | fclose($temp); 668 | }); 669 | 670 | return array( 671 | "name" => self::hashed($meta["uri"]) . "." . $ext, 672 | "size" => filesize($meta["uri"]), 673 | "type" => $mime, 674 | "tmp_name" => $meta["uri"], 675 | "error" => 0 676 | ); 677 | } 678 | 679 | /** 680 | * Hashed text 681 | * @param string $filename 682 | * @return string 683 | */ 684 | public static function hashed($filename) 685 | { 686 | return sha1($filename . "-" . rand(10000, 99999) . "-" . time()); 687 | } 688 | 689 | /** 690 | * Get the data URL by the file path 691 | * https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs 692 | * @param string $filepath 693 | * @return string 694 | */ 695 | public static function data_url($filepath) 696 | { 697 | if (file_exists($filepath)) { 698 | $mime = mime_content_type($filepath); 699 | $source = file_get_contents($filepath); 700 | $encoded = base64_encode($source); 701 | return 'data:' . $mime . ';base64,' . $encoded; 702 | } 703 | return null; 704 | } 705 | } 706 | -------------------------------------------------------------------------------- /tests/CustomErrorsTest.php: -------------------------------------------------------------------------------- 1 | "foo.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/foo.png", "error" => 0, "size" => 1]); 10 | $upload->allowed_extensions(array("baz")); 11 | $upload->error_messages(array( 12 | $upload::ERR_LONG_SIZE => "Fil3 siz3 is t00 l0ng!", 13 | $upload::ERR_INVALID_EXT => "Inv4lid 3xt3nsi0n!", 14 | )); 15 | $upload->check(); 16 | 17 | $this->assertEquals("Inv4lid 3xt3nsi0n!", $upload->get_error()); 18 | } 19 | 20 | public function testLongSize() 21 | { 22 | $upload = new \iamdual\Uploader(["name" => "foo.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/foo.png", "error" => 0, "size" => 999999]); 23 | $upload->max_size(0.1); 24 | $upload->error_messages(array( 25 | $upload::ERR_LONG_SIZE => "Fil3 siz3 is t00 l0ng!", 26 | $upload::ERR_INVALID_EXT => "Inv4lid 3xt3nsi0n!", 27 | )); 28 | $upload->check(); 29 | 30 | $this->assertEquals("Fil3 siz3 is t00 l0ng!", $upload->get_error()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/FileExtensionTest.php: -------------------------------------------------------------------------------- 1 | "foo.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/foo.png", "error" => 0, "size" => 1]); 10 | $upload->allowed_extensions(array("png", "gif")); 11 | $upload->check(); 12 | 13 | $this->assertEquals(null, $upload->get_error(false)); 14 | } 15 | 16 | public function testExtension2() 17 | { 18 | $upload = new \iamdual\Uploader(["name" => "foo.json", "type" => "application/json", "tmp_name" => __DIR__ . "/assets/foo.json", "error" => 0, "size" => 1]); 19 | $upload->allowed_extensions(array("png", "gif")); 20 | $upload->check(); 21 | 22 | $this->assertEquals($upload::ERR_INVALID_EXT, $upload->get_error(false)); 23 | } 24 | 25 | public function testExtension3() 26 | { 27 | $upload = new \iamdual\Uploader(["name" => "foo.json", "type" => "application/json", "tmp_name" => __DIR__ . "/assets/foo.json", "error" => 0, "size" => 1]); 28 | $upload->disallowed_extensions(array("png", "gif")); 29 | $upload->check(); 30 | 31 | $this->assertEquals(null, $upload->get_error(false)); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/FileMimeTypeTest.php: -------------------------------------------------------------------------------- 1 | "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 1]); 10 | $upload->allowed_types(array("image/jpeg", "image/png")); 11 | $upload->check(); 12 | 13 | $this->assertEquals(null, $upload->get_error(false)); 14 | } 15 | 16 | public function testMimeType2() 17 | { 18 | $upload = new \iamdual\Uploader(["name" => "foo.json", "type" => "application/json", "tmp_name" => __DIR__ . "/assets/foo.json", "error" => 0, "size" => 1]); 19 | $upload->allowed_types(array("image/jpeg", "image/png")); 20 | $upload->check(); 21 | 22 | $this->assertEquals($upload::ERR_INVALID_TYPE, $upload->get_error(false)); 23 | } 24 | 25 | public function testMimeType3() 26 | { 27 | $upload = new \iamdual\Uploader(["name" => "foo.json", "type" => "application/json", "tmp_name" => __DIR__ . "/assets/foo.json", "error" => 0, "size" => 1]); 28 | $upload->disallowed_types(array("image/jpeg", "image/png")); 29 | $upload->check(); 30 | 31 | $this->assertEquals(null, $upload->get_error(false)); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/FileNameTest.php: -------------------------------------------------------------------------------- 1 | "foo.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/foo.png", "error" => 0, "size" => 1]); 10 | $upload->name("bar"); 11 | 12 | $this->assertEquals("bar.png", $upload->get_name()); 13 | } 14 | 15 | public function testName2() 16 | { 17 | $upload = new \iamdual\Uploader(["name" => "foo.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/foo.png", "error" => 0, "size" => 1]); 18 | 19 | $this->assertEquals("foo.png", $upload->get_name()); 20 | } 21 | 22 | public function testName3() 23 | { 24 | $upload = new \iamdual\Uploader(["name" => "foo.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/foo.png", "error" => 0, "size" => 1]); 25 | $upload->name("bar.xyz", false); 26 | 27 | $this->assertEquals("bar.xyz", $upload->get_name()); 28 | } 29 | 30 | public function testName4() 31 | { 32 | $upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.json", "error" => 0, "size" => 1]); 33 | $upload->name("foo"); 34 | 35 | $this->assertEquals("foo.jpg", $upload->get_name()); 36 | } 37 | 38 | public function testName5() 39 | { 40 | $upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.json", "error" => 0, "size" => 1]); 41 | $upload->name("foo"); 42 | $upload->encrypt_name(); 43 | 44 | $this->assertNotEquals("foo.json", $upload->get_name()); 45 | $this->assertEquals(false, $upload->encrypt_name); // Because it's setting to 'true' once 46 | } 47 | 48 | public function testName6() 49 | { 50 | $upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.json", "error" => 0, "size" => 1]); 51 | $upload->name("folder1/folder2/foo.xyz", false); 52 | 53 | $this->assertEquals("folder1/folder2/foo.xyz", $upload->get_name()); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /tests/FilePathTest.php: -------------------------------------------------------------------------------- 1 | "foo.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/foo.png", "error" => 0, "size" => 1]); 10 | $upload->path("xyz/abc"); 11 | $upload->name("hello"); 12 | 13 | $this->assertEquals("hello.png", $upload->get_name()); 14 | $this->assertEquals("xyz/abc/", $upload->get_path(null, false)); 15 | $this->assertEquals("xyz/abc/456.png", $upload->get_path("456.png")); 16 | } 17 | 18 | public function testPath2() 19 | { 20 | $upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 1]); 21 | $upload->path("animals/cats"); 22 | $upload->name("2020/08/Tekir"); 23 | 24 | $this->assertEquals("2020/08/Tekir.jpg", $upload->get_name()); 25 | $this->assertEquals("animals/cats/", $upload->get_path(null, false)); 26 | $this->assertEquals("animals/cats/456.jpeg", $upload->get_path("456.jpeg")); 27 | } 28 | } -------------------------------------------------------------------------------- /tests/FileSizeTest.php: -------------------------------------------------------------------------------- 1 | "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 94371840]); 10 | $upload->max_size(1); 11 | $upload->check(); 12 | 13 | $this->assertEquals($upload::ERR_LONG_SIZE, $upload->get_error(false)); 14 | } 15 | 16 | public function testMaxSize2() 17 | { 18 | $upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 1048576]); 19 | $upload->max_size(1); 20 | $upload->check(); 21 | 22 | $this->assertEquals(null, $upload->get_error(false)); 23 | } 24 | 25 | public function testMaxSize3() 26 | { 27 | $upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 2097152]); 28 | $upload->max_size(10); 29 | $upload->check(); 30 | 31 | $this->assertEquals(null, $upload->get_error(false)); 32 | } 33 | 34 | public function testMinSize1() 35 | { 36 | $upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 2097152]); 37 | $upload->min_size(4); 38 | $upload->check(); 39 | 40 | $this->assertEquals($upload::ERR_SMALL_SIZE, $upload->get_error(false)); 41 | } 42 | 43 | public function testMinSize2() 44 | { 45 | $upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 2097152]); 46 | $upload->min_size(2); 47 | $upload->check(); 48 | 49 | $this->assertEquals(null, $upload->get_error(false)); 50 | } 51 | 52 | public function testMinSize3() 53 | { 54 | $upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 20971520]); 55 | $upload->min_size(5); 56 | $upload->check(); 57 | 58 | $this->assertEquals(null, $upload->get_error(false)); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /tests/ImageAspectRatiosTest.php: -------------------------------------------------------------------------------- 1 | "ratio_3_2.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/ratio_3_2.png", "error" => 0, "size" => 1]); 10 | $upload->aspect_ratios(array("3:2", "19:1"))->check(); 11 | $this->assertEquals(null, $upload->get_error(false)); 12 | $upload->aspect_ratios(array(3/2, 19/1))->check(); 13 | $this->assertEquals(null, $upload->get_error(false)); 14 | $upload->aspect_ratios(array(1.5, 19))->check(); 15 | $this->assertEquals(null, $upload->get_error(false)); 16 | } 17 | 18 | public function testAspectRatios2() 19 | { 20 | $upload = new \iamdual\Uploader(["name" => "ratio_4_3.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/ratio_4_3.png", "error" => 0, "size" => 1]); 21 | $upload->aspect_ratios(array("3:2", "4:3"))->check(); 22 | $this->assertEquals(null, $upload->get_error(false)); 23 | $upload->aspect_ratios(array(3/2, 4/3))->check(); 24 | $this->assertEquals(null, $upload->get_error(false)); 25 | } 26 | 27 | public function testAspectRatios3() 28 | { 29 | $upload = new \iamdual\Uploader(["name" => "ratio_9_16.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/ratio_9_16.png", "error" => 0, "size" => 1]); 30 | $upload->aspect_ratios(array("3:2", "4:3", "9:16"))->check(); 31 | $this->assertEquals(null, $upload->get_error(false)); 32 | $upload->aspect_ratios(array(3/2, 4/3, 9/16))->check(); 33 | $this->assertEquals(null, $upload->get_error(false)); 34 | } 35 | 36 | public function testAspectRatios4() 37 | { 38 | $upload = new \iamdual\Uploader(["name" => "ratio_16_9.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/ratio_16_9.png", "error" => 0, "size" => 1]); 39 | $upload->aspect_ratios(array("3:2", "16:9", "4:3"))->check(); 40 | $this->assertEquals(null, $upload->get_error(false)); 41 | $upload->aspect_ratios(array(3/2, 16/9, 4/3))->check(); 42 | $this->assertEquals(null, $upload->get_error(false)); 43 | } 44 | 45 | public function testAspectRatios5() 46 | { 47 | $upload = new \iamdual\Uploader(["name" => "ratio_9_16.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/ratio_9_16.png", "error" => 0, "size" => 1]); 48 | $upload->aspect_ratios(array("9:16"))->check(); 49 | $this->assertEquals(null, $upload->get_error(false)); 50 | $upload->aspect_ratios(array(9/16))->check(); 51 | $this->assertEquals(null, $upload->get_error(false)); 52 | } 53 | 54 | public function testAspectRatios6() 55 | { 56 | $upload = new \iamdual\Uploader(["name" => "ratio_9_16.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/ratio_9_16.png", "error" => 0, "size" => 1]); 57 | $upload->aspect_ratios(array("3:2", "4:3"))->check(); 58 | $this->assertEquals($upload::ERR_ASPECT_RATIO, $upload->get_error(false)); 59 | $upload->aspect_ratios(array(3/2, 4/3))->check(); 60 | $this->assertEquals($upload::ERR_ASPECT_RATIO, $upload->get_error(false)); 61 | } 62 | 63 | public function testAspectRatios7() 64 | { 65 | $upload = new \iamdual\Uploader(["name" => "ratio_3_2.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/ratio_3_2.png", "error" => 0, "size" => 1]); 66 | $upload->aspect_ratios(array("3!^+2", "3::2", ":::"))->check(); 67 | $this->assertEquals($upload::ERR_ASPECT_RATIO, $upload->get_error(false)); 68 | } 69 | } -------------------------------------------------------------------------------- /tests/ImageDimensionsTest.php: -------------------------------------------------------------------------------- 1 | "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 1]); 10 | $upload->max_dimensions(200, 50); 11 | $upload->check(); 12 | 13 | $this->assertEquals($upload::ERR_MAX_DIMENSION, $upload->get_error(false)); 14 | } 15 | 16 | public function testMaxImageDimension2() 17 | { 18 | $upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 1]); 19 | $upload->max_dimensions(210, 20); 20 | $upload->check(); 21 | 22 | $this->assertEquals($upload::ERR_MAX_DIMENSION, $upload->get_error(false)); 23 | } 24 | 25 | public function testMaxImageDimension3() 26 | { 27 | $upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 1]); 28 | $upload->max_dimensions(210, 60); 29 | $upload->check(); 30 | 31 | $this->assertEquals(null, $upload->get_error(false)); 32 | } 33 | 34 | public function testMaxImageDimension4() 35 | { 36 | $upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 1]); 37 | $upload->max_dimensions(400, 80); 38 | $upload->check(); 39 | 40 | $this->assertEquals(null, $upload->get_error(false)); 41 | } 42 | 43 | public function testMinImageDimension1() 44 | { 45 | $upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 1]); 46 | $upload->min_dimensions(300, 20); 47 | $upload->check(); 48 | 49 | $this->assertEquals($upload::ERR_MIN_DIMENSION, $upload->get_error(false)); 50 | } 51 | 52 | public function testMinImageDimension2() 53 | { 54 | $upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 1]); 55 | $upload->min_dimensions(210, 80); 56 | $upload->check(); 57 | 58 | $this->assertEquals($upload::ERR_MIN_DIMENSION, $upload->get_error(false)); 59 | } 60 | 61 | public function testMinImageDimension3() 62 | { 63 | $upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 1]); 64 | $upload->min_dimensions(210, 60); 65 | $upload->check(); 66 | 67 | $this->assertEquals(null, $upload->get_error(false)); 68 | } 69 | 70 | public function testMinImageDimension4() 71 | { 72 | $upload = new \iamdual\Uploader(["name" => "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 1]); 73 | $upload->min_dimensions(100, 20); 74 | $upload->check(); 75 | 76 | $this->assertEquals(null, $upload->get_error(false)); 77 | } 78 | } -------------------------------------------------------------------------------- /tests/MethodsTest.php: -------------------------------------------------------------------------------- 1 | "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 1]); 10 | 11 | $this->assertNotNull($upload->get_tmp_name()); 12 | } 13 | 14 | public function testGetDataUrl() 15 | { 16 | $upload = new \iamdual\Uploader(["name" => "foo.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/foo.png", "error" => 0, "size" => 1]); 17 | 18 | $this->assertEquals(trim(file_get_contents(__DIR__ . "/assets/foo.base64")), $upload->get_data_url()); 19 | } 20 | 21 | public function testGetType() 22 | { 23 | $upload = new \iamdual\Uploader(["name" => "foo.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/foo.png", "error" => 0, "size" => 1]); 24 | 25 | $this->assertEquals("image/png", $upload->get_type()); 26 | } 27 | 28 | public function testGetSize() 29 | { 30 | $upload = new \iamdual\Uploader(["name" => "foo.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/foo.png", "error" => 0, "size" => 1]); 31 | 32 | $this->assertEquals(1, (int)$upload->get_size()); 33 | } 34 | 35 | public function testValidateAspectRatio() 36 | { 37 | $this->assertTrue(\iamdual\Uploader::validate_aspect_ratio("3:2", 300, 200)); 38 | $this->assertTrue(\iamdual\Uploader::validate_aspect_ratio(3/2, 300, 200)); 39 | $this->assertTrue(\iamdual\Uploader::validate_aspect_ratio(1.5, 300, 200)); 40 | $this->assertTrue(\iamdual\Uploader::validate_aspect_ratio("1:1", 300, 300)); 41 | $this->assertTrue(\iamdual\Uploader::validate_aspect_ratio(1, 300, 300)); 42 | $this->assertTrue(\iamdual\Uploader::validate_aspect_ratio("16:9", 1920, 1080)); 43 | $this->assertTrue(\iamdual\Uploader::validate_aspect_ratio(16/9, 1920, 1080)); 44 | $this->assertFalse(\iamdual\Uploader::validate_aspect_ratio("1:1", 300, 300.1)); 45 | $this->assertFalse(\iamdual\Uploader::validate_aspect_ratio(1, 300, 300.1)); 46 | $this->assertFalse(\iamdual\Uploader::validate_aspect_ratio("1:1", 300, 399)); 47 | $this->assertFalse(\iamdual\Uploader::validate_aspect_ratio(1, 300, 399)); 48 | } 49 | } -------------------------------------------------------------------------------- /tests/MustBeImageTest.php: -------------------------------------------------------------------------------- 1 | "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.json", "error" => 0, "size" => 1]); 10 | $upload->must_be_image(); 11 | $upload->check(); 12 | 13 | $this->assertEquals($upload->get_error(false), $upload::ERR_NOT_AN_IMAGE); 14 | } 15 | 16 | public function testMustBeImage2() 17 | { 18 | $upload = new \iamdual\Uploader(["name" => "foo.img", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/foo.png", "error" => 0, "size" => 1]); 19 | $upload->must_be_image(); 20 | $upload->check(); 21 | 22 | $this->assertEquals($upload->get_error(false), null); 23 | } 24 | 25 | public function testMustBeImage3() 26 | { 27 | $upload = new \iamdual\Uploader(["name" => "foo.jpeg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 1]); 28 | $upload->must_be_image(); 29 | $upload->check(); 30 | 31 | $this->assertEquals($upload->get_error(false), null); 32 | } 33 | } -------------------------------------------------------------------------------- /tests/UploadTest.php: -------------------------------------------------------------------------------- 1 | "foo.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 1]); 13 | $upload->must_be_image(); 14 | $upload->path(__DIR__ . "/files"); 15 | $upload->name("bar"); 16 | 17 | $this->assertEquals(true, $upload->upload(true)); 18 | $this->assertEquals(true, $upload->check()); 19 | $suffix = $i > 1 ? "_{$i}" : ""; 20 | $this->assertEquals("bar{$suffix}.jpg", $upload->get_name()); 21 | $uploaded_files[] = $upload->get_path(); 22 | } 23 | 24 | foreach ($uploaded_files as $file) { 25 | @unlink($file); 26 | } 27 | } 28 | 29 | public function testUploadFiles2() 30 | { 31 | $uploaded_files = []; 32 | 33 | for ($i = 1; $i <= 10; $i++) { 34 | $upload = new \iamdual\Uploader(["name" => "xyz.jpg", "type" => "image/jpeg", "tmp_name" => __DIR__ . "/assets/foo.jpg", "error" => 0, "size" => 1]); 35 | $upload->must_be_image(); 36 | $upload->path(__DIR__ . "/files"); 37 | 38 | $this->assertEquals(true, $upload->upload(true)); 39 | $this->assertEquals(true, $upload->check()); 40 | $suffix = $i > 1 ? "_{$i}" : ""; 41 | $this->assertEquals("xyz{$suffix}.jpg", $upload->get_name()); 42 | $uploaded_files[] = $upload->get_path(); 43 | } 44 | 45 | foreach ($uploaded_files as $file) { 46 | @unlink($file); 47 | } 48 | } 49 | 50 | public function testFromBase64() 51 | { 52 | $base64_data = base64_encode(file_get_contents(__DIR__ . "/assets/foo.jpg")); 53 | $upload = new \iamdual\Uploader(\iamdual\Uploader::from_base64($base64_data)); 54 | $upload->allowed_extensions(array("jpg", "jpeg")); 55 | $upload->allowed_types(array("image/jpeg")); 56 | $upload->max_dimensions(210, 60); 57 | $upload->min_dimensions(210, 60); 58 | $upload->name("hello.jpg", false); 59 | $this->assertEquals(true, $upload->upload(true)); 60 | $this->assertEquals(true, $upload->check()); 61 | $this->assertEquals("hello.jpg", $upload->get_name()); 62 | @unlink($upload->get_path("hello.jpg")); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /tests/assets/foo.base64: -------------------------------------------------------------------------------- 1 | data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyBAMAAABYG2ONAAAAFVBMVEUAAAD///9/f39fX1+fn58fHx8/Pz8rYiDqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAo0lEQVRIie2Qyw7CIBBFb2DwO5q+1o0L1w0NrrHRPQnV//8EAUl0ga1ujIs5CZAz4YYZAIZhmN/QQOkjzq3LLuv6xUrQHmTJGphcEGE9rUQ3Y4bqqrDjAlgQoJK9Z8YBmFy8Gp8DeSeTfRSBCf2I6/JN5ORiRfrNiIfqh9S9SVPL27A1C0G4EX2eJR7J1iI7rbG0Vf4x0UwPW0Uh3i0bwzD/yR11mBj1DIKiVwAAAABJRU5ErkJggg== 2 | -------------------------------------------------------------------------------- /tests/assets/foo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iamdual/uploader/e4d550e8980c8f33a90d8f66fc190fc37ef9cb42/tests/assets/foo.jpg -------------------------------------------------------------------------------- /tests/assets/foo.json: -------------------------------------------------------------------------------- 1 | { 2 | "foo": "bar" 3 | } 4 | -------------------------------------------------------------------------------- /tests/assets/foo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iamdual/uploader/e4d550e8980c8f33a90d8f66fc190fc37ef9cb42/tests/assets/foo.png -------------------------------------------------------------------------------- /tests/assets/ratio_16_9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iamdual/uploader/e4d550e8980c8f33a90d8f66fc190fc37ef9cb42/tests/assets/ratio_16_9.png -------------------------------------------------------------------------------- /tests/assets/ratio_3_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iamdual/uploader/e4d550e8980c8f33a90d8f66fc190fc37ef9cb42/tests/assets/ratio_3_2.png -------------------------------------------------------------------------------- /tests/assets/ratio_4_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iamdual/uploader/e4d550e8980c8f33a90d8f66fc190fc37ef9cb42/tests/assets/ratio_4_3.png -------------------------------------------------------------------------------- /tests/assets/ratio_9_16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iamdual/uploader/e4d550e8980c8f33a90d8f66fc190fc37ef9cb42/tests/assets/ratio_9_16.png --------------------------------------------------------------------------------