上传临时媒体文件.   $file 为  /logo/img.jpeg 服务器上图片路径

@author Tian

@date   2015-10-10

@param string $file 文件路径
@param string $type 文件类型

@return bool|array 接口返回结果
979374
public function upload($file, $type)
    {
        if (!$file || !$type) {
            $this->setError('参数缺失');

            return false;
        }

        if (!file_exists($file)) {
            $this->setError('文件路径不正确');

            return false;
        }

        $data          = [];
        $data['media'] = '@' . realpath($file);

        Api::setPostQueryStr('type', $type);

        $node = 'upload';

        $res = $this->_post($node, $data, false);

        return $res;
    }
954162
public function accept( File $file ) : bool
    {
        $res = @getimagesize($file);
        if (!$res || !is_array($res)){
            return false;
        }
        $imagetype = $res[2] ?? 0;
        return in_array($imagetype, $this->image_types);
    }
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;
    }
1103994
private function createMediaFileGedcom(string $file, string $type, string $title): string
    {
        if (preg_match('/\.([a-z0-9]+)/i', $file, $match)) {
            $extension = strtolower($match[1]);
            $extension = str_replace('jpg', 'jpeg', $extension);
            $extension = ' ' . $extension;
        } else {
            $extension = '';
        }

        $gedcom = '1 FILE ' . $file;
        if ($type !== '') {
            $gedcom .= "\n2 FORM" . $extension . "\n3 TYPE " . $type;
        }
        if ($title !== '') {
            $gedcom .= "\n2 TITL " . $title;
        }

        return $gedcom;
    }
594725
public function open($type = 'image/jpeg')
    {
        switch ($type) {
            case 'image/jpeg':
                $image = imagecreatefromjpeg($this->file);
                break;
            case 'image/gif':
                $image = imagecreatefromgif($this->file);
                break;
            case 'image/png':
                $image = imagecreatefrompng($this->file);
                break;
            default:
                $image = null;
        }
        return $this->image = $image;
    }
675328
protected function getCachePath($file, $type): array
    {
        $hash = md5($file . $type);
        if (isset($this->config[$type])) {
            $isTransparent = ArrayHelper::getValue($this->config[$type], 'transparent', false);
        } else {
            $isTransparent = false;
        }

        $cacheFileExt = $isTransparent ? self::TRANSPARENT_EXTENSION : strtolower(pathinfo($file, PATHINFO_EXTENSION));
        $cachePath = $this->cachePath . $hash[0] . DIRECTORY_SEPARATOR;
        $cachePublicPath = $this->cachePublicPath . $hash[0] . DIRECTORY_SEPARATOR;
        $cacheFile = "{$hash}.{$cacheFileExt}";
        $systemPath = Yii::getAlias('@app') . $cachePath;

        FileHelper::createDirectory($systemPath);

        return [
            'system' => $systemPath . $cacheFile,
            'web' => $cachePath . $cacheFile,
            'public' => $cachePublicPath . $cacheFile,
            'extension' => $cacheFileExt,
        ];
    }
1003049
public function uploadheadimg($file, $kf_account)
    {
        if (!$file || !$kf_account) {
            $this->setError('参数缺失');

            return false;
        }

        if (!file_exists($file)) {
            $this->setError('文件路径不正确');

            return false;
        }

        $data          = [];
        $data['media'] = '@' . realpath($file);

        $this->setPostQueryStr('kf_account', $kf_account);

        $this->apitype = 'customservice';
        $this->module  = 'kfaccount';

        $res = $this->_post('uploadheadimg', $data, false);

        return $res;
    }
1128881
public function load($file, $type = null)
    {
        $path = $this->locator->locate($file);

        $xml = $this->parseFile($path);
        $xml->registerXPathNamespace('config', 'http://thelia.net/schema/dic/config');

        $this->removeScope($xml);

        $this->container->addResource(new FileResource($path));

        $this->parseLoops($xml);

        $this->parseFilters($xml);

        $this->parseTemplateDirectives($xml);

        $this->parseParameters($xml);

        $this->parseCommands($xml);

        $this->parseForms($xml);

        $this->parseDefinitions($xml, $path);

        $this->parseHooks($xml, $path, $type);

        $this->propelOnlyRun(
            [$this, "parseExportCategories"],
            $xml
        );

        $this->propelOnlyRun(
            [$this, "parseExports"],
            $xml
        );

        $this->propelOnlyRun(
            [$this, "parseImportCategories"],
            $xml
        );

        $this->propelOnlyRun(
            [$this, "parseImports"],
            $xml
        );
    }
722138
public function getFileType($ext, $type = '')
    {
        // 可上传类型
        $upload_type = $this->getUploadType();
        
        // 没有指定上传类型
        if ($type == '') {
            foreach ($upload_type as $c => $v) {
                if (in_array($ext, $v)) {
                    $type = $c;
                    break;
                }
            }
        }
        
        // 验证上传类型
        if ($type == '' || ! isset($upload_type[$type]) || ! in_array($ext, $upload_type[$type])) {
            return '';
        } else {
            return $type;
        }
    }
127787
public function javascriptMinify($file, $type = 'file')
    {
        $file = $this->getFile($file, $type);
        $file = preg_replace(["/\/\/(.*\s+)/", "/;+\}/"], ['', '}'], $file);
        $file = $this->cleanSpaces($file);
        $file = $this->removeComments($file);

        return $file;
    }
1204442
final protected function createImageFromFile($file, $type)
    {
        switch ($type) {
            case \IMAGETYPE_GIF:
                return imagecreatefromgif($file);

            case \IMAGETYPE_JPEG:
                return imagecreatefromjpeg($file);

            case \IMAGETYPE_PNG:
                return imagecreatefrompng($file);
    
            default:
                throw new LogicException(sprintf('Can not create image from "%s"', $file));
        }
    }
196839
public function uploadByPath($path, $type = '', $option = [])
    {
        // 文件是否存在
        if (empty($path) || ! is_file($path)) {
            return Format::formatResult(0, '文件不存在');
        }
        
        // 没有指定后缀
        if (! isset($option['ext'])) {
            $option['ext'] = FileInfo::getFileExt($path);
        }
        
        // 类型识别出错
        if (empty($option['ext']) && isset($option['mime'])) {
            $option['ext'] = FileInfo::mimeToExt($option['mime']);
        }
        
        // 上传文件类型
        $type = $this->getFileType($option['ext'], $type);
        if (empty($type)) {
            return Format::formatResult(0, '不合法的文件类型');
        }
        
        // 处理图片
        $type == 'image' && $this->processImage($path, $option);
        
        // 没有文件Md5
        if (! isset($option['hash'])) {
            $option['hash'] = md5_file($path);
        }
        
        // 是否上传过
        if ($this->onCheck && is_callable($this->onCheck)) {
            $url = call_user_func($this->onCheck, [
                'hash' => $option['hash'],
                'type' => $type,
                'ext' => $option['ext'],
                'path' => $path
            ]);
            
            // 文件存在
            if (! empty($url)) {
                return Format::formatResult(1, '上传成功', [
                    'url' => $url,
                    'hash' => $option['hash'],
                    'type' => $type,
                    'ext' => $option['ext'],
                    'path' => $this->option('root') . DS . $type . DS . $option['hash'] . '.' . $option['ext']
                ]);
            }
        }
        
        // 上传文件
        $target_path = $type . '/' . $option['hash'] . '.' . $option['ext'];
        $res = $this->uploadFile($path, $target_path);
        
        // 上传完成
        $this->onDone && is_callable($this->onDone) && call_user_func($this->onDone, [
            'hash' => $option['hash'],
            'type' => $type,
            'ext' => $option['ext'],
            'path' => $path
        ]);
        
        // 上传失败
        if ($res['code'] != 1) {
            $this->onError && is_callable($this->onError) && call_user_func($this->onError, [
                'hash' => $option['hash'],
                'type' => $type,
                'ext' => $option['ext'],
                'path' => $path
            ]);
            return $res;
        }
        
        // 上传成功
        $this->onSuccess && is_callable($this->onSuccess) && call_user_func($this->onSuccess, [
            'hash' => $option['hash'],
            'type' => $type,
            'ext' => $option['ext'],
            'path' => $path,
            'url' => $res['data']
        ]);
        
        // 返回结果
        return Format::formatResult(1, '上传成功', [
            'url' => $res['data'],
            'hash' => $option['hash'],
            'type' => $type,
            'ext' => $option['ext'],
            'path' => $this->option('root') . DS . $type . DS . $option['hash'] . '.' . $option['ext']
        ]);
    }
315629
public static function createDate(String $file, String $type = 'd.m.Y G:i:s') : String
    {
        $file = self::rpath($file);

        if( ! file_exists($file) )
        {
            throw new Exception\FileNotFoundException(NULL, $file);
        }

        $date = filectime($file);

        return date($type, $date);
    }
1033132
public function blob( $type = 'PNG' ) {

        $type = strtoupper($type);
        if ( ImageType::GIF == $type ) {

            imagegif( $this->gd );

        } else if ( ImageType::JPEG == $type ) {

            imagejpeg( $this->gd );

        } else if ( ImageType::PNG == $type ) {

            imagepng( $this->gd );

        } else if ( ImageType::WBMP == $type ) {

            imagewbmp( $this->gd );

        } else {
            throw new \Exception( sprintf( 'File type "%s" not supported.', $type ) );
        }
    }
892426
public function uploadLocal($file)
    {
        // 格式 uploads/image/$folder/2018/02/02/
        $folderName = $this->rootFolder . $this->folder . DIRECTORY_SEPARATOR . date($this->dateFormat, time());

        // 将 public 目录的绝对路径和 上面定义的路径拼接到一起
        // 格式: /www/www.broqiang.com/public/uploads/image/$folder/2018/02/02/
        $uploadPath = public_path($folderName);

        // 拼接文件名
        $filename = $this->getFilename($file);

        // 将图片移动到我们的目标存储路径中
        $file->move($uploadPath, $filename);

        if ($this->maxWidth && $this->extension != 'gif') {
            $this->reduceSize($uploadPath . DIRECTORY_SEPARATOR . $filename, $this->maxWidth);
        }

        return $this->getUrlPath($folderName, $filename);
    }
371225
public function change_perm_by_real($file, $file_new, $type='file'){
    if ( !empty($file) &&
      !empty($file_new) &&
      file_exists($file_new) &&
      ($id_opt = $this->real_to_perm($file, $type)) &&
      !$this->real_to_perm($file_new, $type)
    ){
      $is_file = $type === 'file';
      $code = $is_file ? bbn\str::file_ext(basename($file_new), 1)[0] : basename($file_new).'/';
      if ( ($id_parent = $this->create_perm_by_real(dirname($file_new).'/', 'dir'))
      ){
        $this->options->set_prop($id_opt, ['code' => $code]);
        $this->options->move($id_opt, $id_parent);
        return true;
      }
    }
    return false;
  }
1266571
protected function read($file, $type = 'raw', array $data = [])
    {
        $type = strtolower($type);

        if ($type === 'include' || $type === 'require') {
            ob_start();

            extract($data);
            unset($data);

            if ($type === 'include') {
                include $file;
            } else {
                require $file;
            }

            return ob_get_clean();
        }

        $raw = is_readable($file) === true ? file_get_contents($file) : $file;

        if ($type === 'base64') {
            return base64_encode($raw);
        }

        return $raw;
    }