Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
/ Yii-UploadedFile Public archive

A wrapper for CUploadedFile class of PHP Yii framework.

License

Notifications You must be signed in to change notification settings

yanli0303/Yii-UploadedFile

Repository files navigation

Yii-UploadedFile

By Yan Li

Build Status Coverage Status License PayPayl donate button

A wrapper for CUploadedFile class of PHP Yii framework. It adds following help methods to CUploadedFile class:

  • isExtensionInList($extensions)
  • isMimeTypeInList($mimeTypes)
  • isImageTypeInList($imageTypes)
  • validateImageDimensions($maxWidth, $maxHeight, $minWidth, $minHeight)
  • validate($maxFileBytes, $allowedExtensions, $allowedMimeTypes)
  • validateImage($maxFileBytes, $allowedExtensions, $allowedImageTypes, $maxWidth = null, $maxHeight = null, $minWidth = null, $minHeight = null)
  • saveImage($saveAs, $pngToJpg = false)

Usage

$maxBytes = 4194304; //4 * 1024 * 1024 = 4MB
$allowedExtensions = array('.png', '.jpg', '.jpeg');
$allowedTypes = array(IMAGETYPE_JPEG, IMAGETYPE_PNG);

$uploaded = new UploadedFile('file');
$error = $uploaded->validateImage($maxBytes, $allowedExtensions, $allowedTypes);
if (is_string($error)) {
    throw new Exception($error);
}

$saveAs = '/webroot/uploads/images/'.basename($uploaded->file->getName());
$saved = $uploaded->saveImage($saveAs, false);
if (empty($saved)) {
    throw new Exception('Sorry, we couldn\'t upload the image.');
}

// do sth with saved image: $saved

About

A wrapper for CUploadedFile class of PHP Yii framework.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages