验证上传文件后缀
@access protected
@param mixed     $file  上传文件
@param mixed     $rule  验证规则
@return bool
353675
protected function fileExt($file, $rule)
    {
        if (!($file instanceof File)) {
            return false;
        }
        if (is_string($rule)) {
            $rule=explode(',', $rule);
        }
        if (is_array($file)) {
            foreach ($file as $item) {
                if (!$item->checkExt($rule)) {
                    return false;
                }
            }
            return true;
        } else {
            return $file->checkExt($rule);
        }
    }
1114309
public static function getFileMimeType($file)
    {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        if ($finfo) {
            $mime_type = finfo_file($finfo, $file);
            finfo_close($finfo);
            return $mime_type;
        }
        return false;
    }
82621
private function lintFile($file)
    {
        // first try to parse it
        try {
            $this->parser->parse(\file_get_contents($file));
        } catch (\Exception $e) {
            return false;
        }

        return true;
    }
1238193
public function move($file) {
        $result = true;
        $this->map(function (FileObject $item) use (&$result, $file) {
            $result = $result && $item->move($file. '/'. $item->getName());
        });
        return $result;
    }
200502
protected function length($value, $rule)
    {
        if (is_array($value)) {
            $length=count($value);
        } elseif ($value instanceof File) {
            $length=$value->getSize();
        } else {
            $length=mb_strlen((string) $value);
        }

        if (strpos($rule, ',')) {
            // 长度区间
            list($min, $max)=explode(',', $rule);
            return $length >= $min && $length <= $max;
        } else {
            // 指定长度
            return $length == $rule;
        }
    }
83301
public function Upload ($file)
	{
		
		// Set uploaded file
		
		$this->upload	= $file;
		
		// If there is an upload error
		
		if ($this->upload['error'] != UPLOAD_ERR_OK)
		{
			
			// Set error
			
			new \Sonic\Message ('error', self::_uploadError ($this->upload['error']));
			
			// Return FALSE
			
			return FALSE;
			
		}
		
		// Set file details
		
		$this->filename		= $this->upload['name'];
		$this->extension	= self::_getExtension ($this->filename);
		$this->name			= self::_getFilename ($this->filename);
		
		// Return TRUE
		
		return TRUE;
		
	}
549129
private function ruleTemplate($rule)
    {
        $file = __DIR__ . "/../templates/validator/rule.php.dist";

        if (file_exists($file))
        {
            $template = strtr(file_get_contents($file), [
                '{{namespace}}' => get_app_namespace(),
                '{{rule}}' => $rule
            ]);

            if (!file_exists(app_path("Validation/Rules")))
            {
                mkdir(app_path("Validation/Rules"), 0755, true);
            }

            $file_path = app_path("Validation/Rules/{$rule}.php");

            $file = fopen($file_path, "w");
            fwrite($file, $template);
            fclose($file);

            return file_exists($file_path);
        }

        return false;
    }
949090
public function addValidationRule($rule, $message = null)
    {
        $uploadRules = ['file', 'image', 'mime', 'size', 'dimensions', 'max', 'min', 'between'];

        foreach ($uploadRules as $uploadRule) {
            if (strpos($rule, $uploadRule) !== false) {
                $this->uploadValidationRules[] = $rule;

                if (is_null($message)) {
                    return $this;
                }

                return $this->addValidationMessage($rule, $message);
            }
        }

        return parent::addValidationRule($rule, $message);
    }
1293457
public function accept( File $file ) : bool
    {
        return $file->isFile() && $file->getExtension() === $this->extension;
    }
873288
private function validate(FileSystem\File $file)
    {
        $filters = $this->buildFilters();
        foreach ($filters as $filter) {
            if (! $filter->validate($file)) return false;
        }

        return true;
    }
1262137
public function parse($value, $rule) {

        $default = isset($rule['default']) ? $rule['default'] : NULL;
        $index = $rule['name'];
        $fileList = array();

        // 未上传 && (有默认值 || 非必须)
        if (!isset($_FILES[$index]) && ($default !== NULL || empty($rule['require']))) {
            return $default;
        }

        if (!isset($_FILES[$index]) || !is_array($_FILES[$index])) {
            $message = isset($rule['message'])
                ? \PhalApi\T($rule['message'])
                : \PhalApi\T('miss upload file: {file}', array('file' => $index));
            throw new BadRequestException($message);
        }

        if (is_array($_FILES[$index]['tmp_name'])) {
            $count = sizeof($_FILES[$index]['tmp_name']);

            for ($i = 0; $i < $count; $i++) {
                $file = array(
                    'name' => $_FILES[$index]['name'][$i],
                    'type' => $_FILES[$index]['type'][$i],
                    'tmp_name' => $_FILES[$index]['tmp_name'][$i],
                    'error' => $_FILES[$index]['error'][$i],
                    'size' => $_FILES[$index]['size'][$i]
                );

                $fileList[] = $this->parseOne($file, $rule);
            }
        } else {
            $file = array(
                'name' => $_FILES[$index]['name'],
                'type' => $_FILES[$index]['type'],
                'tmp_name' => $_FILES[$index]['tmp_name'],
                'error' => $_FILES[$index]['error'],
                'size' => $_FILES[$index]['size']
            );
            // 单个文件直接返回文件信息数组
            return $this->parseOne($file, $rule);
        }

        // 返回文件信息二维数组
        return $fileList;
    }
87588
private function secureCheck($file)
    {
        //文件上传失败，检查错误码
        if ($file['error'] != 0) {
            switch ($file['error']) {
                case 1:
                    $this->errorInfo = '上传的文件大小超过了 php.ini 中 upload_max_filesize 选项限制的值';
                    break;
                case 2:
                    $this->errorInfo = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
                    break;
                case 3:
                    $this->errorInfo = '文件只有部分被上传';
                    break;
                case 4:
                    $this->errorInfo = '没有文件被上传';
                    break;
                case 6:
                    $this->errorInfo = '找不到临时文件夹';
                    break;
                case 7:
                    $this->errorInfo = '文件写入失败';
                    break;
                default:
                    $this->errorInfo = '未知上传错误！';
            }
            return false;
        }

        //文件上传成功，进行自定义检查
        if ( (-1 != $this->config['maxSize']) &&  ($file['size'] > $this->config['maxSize']) ) {
            $this->errorInfo = '上传文件大小不符';
            return false;
        }

        //检查文件Mime类型
        if (!$this->checkType($file['type'])) {
            $this->errorInfo = '上传文件mime类型允许';
            return false;
        }

        //检查文件类型
        if (!$this->checkExt($file['extension'])) {
            $this->errorInfo ='上传文件类型不允许';
            return false;
        }

        //检查是否合法上传
        if (!is_uploaded_file($file['tmp_name'])) {
            $this->errorInfo = '非法的上传文件！';
            return false;
        }
        return true;
    }
853134
final public function hasFile($file)
	{
		$helloReflection = new \ReflectionClass($this);
		$dir = dirname($helloReflection->getFilename());

		if(file_exists($dir . '/' . $file)) {
			return $dir . '/' . $file;
		}
		return false;
	}
332126
public function up(UploadedFile $file)
    {
        //设置文件名称
        $this->oriName = $file->name;
        //设置文件大小
        $this->fileSize = $file->size;
        $this->fileType = $this->getExtension();
        $this->fullName = $this->getFullName();
        $this->filePath = $this->getFilePath();
        $this->fileName = $this->getFileName();
        $dirName = dirname($this->filePath);
        if (!is_dir($dirName)) {//递归创建保存目录
            FileHelper::createDirectory($dirName, $this->dirMode, true);
        }
        if (!($file->saveAs($this->filePath) && file_exists($this->filePath))) {
            $this->stateInfo = Yii::t('attachment', 'An error occurred while saving the file.');
            return false;
        } else { //移动成功
            $this->saveModel();
            $this->stateInfo = 'SUCCESS';
            return true;
        }
    }
912957
protected function checkFile($path, $type, $file)
    {
        $filePath = implode('/', [$path, $type, $file]);

        if (is_file($filePath)) {
            return $filePath;
        } else {
            return false;
        }
    }
635115
private function check($file)
    {
        /* 文件上传失败，捕获错误代码 */
        if ($file['error']) {
            $this->error($file['error']);

            return false;
        }
        /* 无效上传 */
        if (empty($file['name'])) {
            $this->error = '未知上传错误！';
        }
        /* 检查是否合法上传 */
        if (!is_uploaded_file($file['tmp_name'])) {
            $this->error = '非法上传文件！';

            return false;
        }
        /* 检查文件大小 */
        if (!$this->checkSize($file['size'])) {
            $this->error = '上传文件大小不符！';

            return false;
        }
        /* 检查文件Mime类型 */
        //TODO:FLASH上传的文件获取到的mime类型都为application/octet-stream
        if (!$this->checkMime($file['type'])) {
            $this->error = '上传文件MIME类型不允许！';

            return false;
        }
        /* 检查文件后缀 */
        if (!$this->checkExt($file['ext'])) {
            $this->error = '上传文件后缀不允许';

            return false;
        }

        /* 通过检测 */

        return true;
    }
519570
public function loadFile(string $file)
    {
        $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
        switch ($extension) {
            case "ini":
                $this->loadIni($file);
                break;
            case "xml":
                $this->loadXml($file);
                break;
            case "yml":
                $this->loadYaml($file);
                break;
            case "php":
                $this->loadPhp($file);
                break;
            default:
                //不用处理
                break;
        }
    }