PHP code example of struktal / struktal-file-uploads

1. Go to this page and download the library: Download struktal/struktal-file-uploads 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/ */

    

struktal / struktal-file-uploads example snippets


$fileUpload = new FileUpload();

// File Upload Options
$fileUpload->setInputName("fileInputName") // Set the Name of the File Input
             ->setMultiple(false) // Only allow a single File
             ->setAllowedMimeTypes(["image/jpeg", "image/png"]) // Only allow JPEG and PNG Files
             ->setMaxSize(2) // Only allow Files up to 2 MiB
             ->handleUploadedFiles();

// Check if there were Errors during the Upload
if(!($fileUpload->successful())) {
    $errors = $fileUpload->getErrors();
    return;
}

// Get the uploaded File
$uploadedFile = $fileUpload->getFiles();

[
    [0] => [
        "name" => "file.jpeg",
        "type" => "image/jpeg",
        "tmp_name" => "/tmp/php/php1h4j1o",
        "error" => 0,
        "size" => 1024
    ]
]

$fileUpload = new FileUpload();

// File Upload Options
$fileUpload->setInputName("fileInputName") // Set the Name of the File Input
             ->setMultiple(true) // Allow multiple Files
             ->setAllowedMimeTypes(["image/jpeg", "image/png"]) // Only allow JPEG and PNG Files
             ->setMaxSize(2) // Only allow Files up to 2 MiB
             ->handleUploadedFiles();

// Check if there were Errors during the Upload
if(!($fileUpload->successful())) {
    $errors = $fileUpload->getErrors();
    return;
}

// Get the uploaded Files
$uploadedFiles = $fileUpload->getFiles();

[
    [0] => [
        "name" => "file1.jpeg",
        "type" => "image/jpeg",
        "tmp_name" => "/tmp/php/php1h4j1o",
        "error" => 0,
        "size" => 1024
    ],
    [1] => [
        "name" => "file2.jpeg",
        "type" => "image/jpeg",
        "tmp_name" => "/tmp/php/php1h4j1o",
        "error" => 0,
        "size" => 1024
    ],
    // ...
]