Download the PHP package didslm/file-upload-wrapper without Composer
On this page you can find all versions of the php package didslm/file-upload-wrapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download didslm/file-upload-wrapper
More information about didslm/file-upload-wrapper
Files in didslm/file-upload-wrapper
Package file-upload-wrapper
Short Description This wrapper makes file uploading easier to use
License MIT
Informations about the package file-upload-wrapper
File Upload Wrapper
The File Upload Wrapper is a PHP library that simplifies file uploads by providing a set of easy-to-use classes that handle common validation and processing tasks. With this library, you can:
- Validate uploaded files with ease
- Process uploaded files with targeted validations for specific fields
- Simplify the file upload process with a set of easy-to-use classes
Getting Started
To use the library, follow these steps:
-
Install the library using Composer:
-
Import the classes you need:
-
Use the
upload()
method to handle file uploads for your entity: - The same exmaple you can do via Dependency Injection:
At the end of this document you can see how to configure in Laravel or Symfony.
Examples
Handling File Uploads for an Entity
The following code shows an example of how to use the library to handle file uploads for an entity:
In this example, the Product
class has two properties image
and profile
that are decorated with the Image
attribute.
Types
In the following example you will see a list of available Attribute types:
The Image
attribute provides metadata to the library to process the files correctly during the upload.
The Document
attribute provides metadata to the library to process the files correctly during the upload.
The Video
attribute provides metadata to the library to process the files correctly during the upload.
The upload()
method is then called on the Uploader
class with the Product
object and an array of validation rules as its parameters.
Handling Exceptions
The library provides a FileUploadException
class that all exceptions thrown by the library extend. This means that you can catch all exceptions using FileUploadException
in a try-catch block, as shown below:
Validation
The library provides several validation classes that you can use to validate uploaded files. These classes can be passed as parameters to the upload()
method to specify the validation rules for the files being uploaded.
Type
The FileType
class is used to check the file type. You can specify the types of files allowed by passing an array of file types to the constructor. For example:
Size
The FileSize
class is used to validate the file size. You can specify the maximum file size allowed by passing the size in bytes to the constructor. Alternatively, you can use the Size class to specify the size in a more readable format. For example:
Dimension
The Dimension
class is used to validate the dimensions of images. You can specify the maximum width and height of the image by passing them as parameters to the constructor. For example:
Targeted Validations
You can also target specific fields in your entity with a set of validations.
To do this, you can use the FieldValidations
class, which takes the request field name as it's first parameter and an array of validation rules as its second parameter. Here's an example:
In the example above, we are specifying a set of validation checks that apply to the profile field in the Product entity. These checks will only be applied to the profile image uploaded by the user.
Frameworks Implementation
The library is framework agnostic, which means that you can use it with any framework.
The following sections show how to configure the library in some of the most popular frameworks.
Symfony
In your Symfony app you can easily configure the library in your services.yaml
file:
Laravel
In your Laravel app you can easily configure the library in your AppServiceProvider.php
file:
Handling file uploads can be a complicated and error-prone task, but with this library, you can simplify the process and focus on building the features that matter. If you have any questions or feedback, feel free to reach out to the author on Twitter or LinkedIn.