PHP code example of khanzadimahdi / uploadmanager

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

    

khanzadimahdi / uploadmanager example snippets


if($_SERVER['REQUEST_METHOD']=='POST'){
    $uploadManager=new \UploadManager\Upload('media');
    $chunks=$uploadManager->upload('uploads');
    if(!empty($chunks)){
        foreach($chunks as $chunk){
            echo '<p class="success">'.$chunk->getNameWithExtension().' has been uploaded successfully</p>';
        }
    }
}

try{
    if($_SERVER['REQUEST_METHOD']=='POST'){
        $uploadManager=new \UploadManager\Upload('media');
        $chunks=$uploadManager->upload('uploads');
        if(!empty($chunks)){
            foreach($chunks as $chunk){
                echo '<p class="success">'.$chunk->getNameWithExtension().' has been uploaded successfully</p>';
            }
        }
    }
}catch(\UploadManager\Exceptions\Upload $exception){
    //if file exists: (user selects a file)
    if(!empty($exception->getChunk())){
        foreach($exception->getChunk()->getErrors() as $error){
            echo '<p class="error">'.$error.'</p>';
        }
    }else{
        echo '<p class="error">'.$exception->getMessage().'</p>';
    }
}

//add validations
$uploadManager->addValidations([
    new \UploadManager\Validations\Size('2M'), //maximum file size is 2M
    new \UploadManager\Validations\Extension(['jpg','jpeg','png','gif']),
]);

//add callback : remove uploaded chunks on error
$uploadManager->afterValidate(function($chunk){
    $address=($chunk->getSavePath().$chunk->getNameWithExtension());
    if($chunk->hasError() && file_exists($address)){
        //remove current chunk on error
        @unlink($address);
    }
});
 html
<form method="post" action="<?= htmlentities($_SERVER['PHP_SELF'])