PHP code example of rafalimanana / file-validator

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

    

rafalimanana / file-validator example snippets


use Pw\FileValidator\FileValidator

$fileValidator = new FileValidator([]);

use Pw\FileValidator\FileValidator

$allowedMimeTypes = ['image/jpeg', 'image/png'];
$allowedExtensions = ['jpg', 'jpeg', 'png'];
$maxFileSize = 1024 * 1024; // 1 MB

$fileValidator = new FileValidator(
    $allowedMimeTypes, 
    $allowedExtensions, 
    $maxFileSize
);

$file = [
    'name' => 'example.jpg',
    'type' => 'image/jpeg',
    'size' => 500000, // 500 KB
];

if ($fileValidator->validateFile($file)) {
    //Le fichier est valide.
} else {
    //Le fichier n'est pas valide.
}

use Pw\FileValidator\FileValidator

$fileValidator = new FileValidator();

$mimetype = $file->getMimeType();//Type de fichier

if ($fileValidator->isImage($mimetype)) {
    //Le fichier est un image.
} else {
    //Le fichier n'est un image.
}

//ou
if ($fileValidator->isImage($file)) {//Objet de fichier
    //Le fichier est un image.
} else {
    //Le fichier n'est un image.
}

uploadToPath($file, $dir, $file_name)
isImage($file)
isPdf($file)
isDocx($file)
isExcel($file)
isVideo($file)
getFileType($file)
validSize($file, $max_mb=1)
validateMimeType($file)
validateExtension($file)
validateFile($file)

php composer.phar