PHP code example of yanli0303 / yii-uploaded-file
1. Go to this page and download the library: Download yanli0303/yii-uploaded-file 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/ */
yanli0303 / yii-uploaded-file example snippets
$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