PHP code example of didslm / file-upload-wrapper

1. Go to this page and download the library: Download didslm/file-upload-wrapper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

didslm / file-upload-wrapper example snippets


use Didslm\FileUpload\Uploader;
use Didslm\FileUpload\UploaderInterface;
use Didslm\FileUpload\File;
use Didslm\FileUpload\Validation\FileSize;
use Didslm\FileUpload\Validation\FileType;
use Didslm\FileUpload\Validation\Dimension;
use Didslm\FileUpload\FieldValidation;
use Didslm\FileUpload\Attributes\Image;
use Didslm\FileUpload\Attributes\Document;
use Didslm\FileUpload\Exceptions\FileUploadException;

class Product {
    #[Image(requestField: "request_field", dir: "/public")]
    private string $image;
    
    #[Image(requestField: "profile_field", dir: "/public")]
    private string $profile;
    
    // ...
}

$product = new Product();

Uploader::upload($product, [
    new FileType([File::JPEG]),
    new FileSize(2, File::MB)
]);


class ProductController {
    public function __construct(private UploaderInterface $uploader){}
    
    public function upload(Request $request)
    {
        $product = new Product();
        
        $this->uploader->upload($product, [
            new FileType([File::IMAGES]),
            new FileSize(2, File::MB)
        ]);
    }
}

class Product {
    //...
    #[Image(requestField: "request_field", dir: "/public")]
    private string $image;
    
    #[Image(requestField: "profile_field", dir: "/public")]
    private string $profile;
    
    public function getImageFilename(): string
    {
        return $this->image;
    }
    
    public function getProfileFilename(): string
    {
        return $this->profile;
    }
}


#[new Image(requestName: 'file_upload_field', dir: '/upload/dir')]
#[new Document(requestName: 'cv_file', dir: '/upload/dir')]
#[new Video(requestName: 'video_file', dir: '/upload/dir')]

$product = new Product();

Uploader::upload($product, [
    new FileType([File::JPEG]),
    new FileSize(2, File::MB)
]);

echo $product->getImageFilename();


try {
    Uploader::upload($product, [
        new FileType([File::PNG]),
        new FileSize(2, File::MB)
    ]);
} catch (FileUploadException $e) {
    // handle exception
}

new FileType([File::PNG, File::JPEG, File::GIF])

new FileSize(2, File::MB)
new FileSize(200, File::KB)


new Dimension(200, 200)

$profileValidations = new FieldValidations("profile_field", [
    new Dimension(200, 200),
    new FileSize(2, File::MB)
]);

Uploader::upload($product, [
    new FileType([File::PNG, File::JPEG, File::GIF]),
    $profileValidations,
]);

public function register()
{
    $this->app->bind(UploaderInterface::class, function (){
         return UploaderFactory::create();
    });
}