File upload
@param string $field
@param string $path
@param string $type
@param string $name
@return self
971531
public function file($field, $path, $type, $name)
    {
        $name = basename($name);
        if (class_exists('CURLFile')) {
            $this->set('CURLOPT_SAFE_UPLOAD', true);
            $file = curl_file_create($path, $type, $name);
        } else {
            $file = "@{$path};type={$type};filename={$name}";
        }
        return $this->post($field, $file);
    }
1186920
public function createFile($attribute, $path, $name)
    {
        $handlerCreateFile = $this->fileOption($attribute, 'createFile');
        $file = $handlerCreateFile($path, $name);
        if ($file) {
            $storage = $this->fileStorage($attribute);
            $contents = file_get_contents($path);
            $handlerTemplatePath = $this->fileOption($attribute, 'templatePath');
            if ($storage->write($handlerTemplatePath($file), $contents)) {
                $this->setState($attribute, $file);
                $this->owner->{$attribute} = $file->id;
                return $file;
            }
        } // @codeCoverageIgnore
        return false; // @codeCoverageIgnore
    }
1115120
public function upload(string $type, string $path): array
    {
        return $this->httpClient->postFile('media/upload', $path, compact('type'));
    }
1172045
public function create($name, $path, $table = null, $create = false)
    {
        $path = $this->getPath($name, $path);

        // First we will get the stub file for the migration, which serves as a type
        // of template for the migration. Once we have those we will populate the
        // various place-holders, save the file, and run the post create event.
        $stub = $this->getStub($table, $create);

        $this->files->put($path, $this->populateStub($name, $stub, $table));

        $this->firePostCreateHooks();

        return $path;
    }
924438
public function attach($file, $name = null, $type = null)
    {
        if (!$file instanceof MailAttachment) {
            $file = new MailAttachment($file);
        }

        if ($name) {
            $file->setFilename($name);
        }

        if ($type) {
            $file->setContentType($type);
        }

        $this->files[] = $file;

        return $this;
    }
1371057
public function uploadField(UploadField $uploadField)
    {
        $tag = new Tag('input');
        $tag->setAttribute('type', 'file');
        $tag->setAttribute('accept', $uploadField->getAccept());
        if ($uploadField->isMultiple()) {
            $tag->setAttribute('multiple', 'multiple');
        }


        return $this->parseTag($tag, $uploadField);
    }
848540
private function instanciateField(string $name, string $type): FieldInterface
    {
        $fieldClass = $this->getFieldClass($type);
        $field = new $fieldClass($name);

        if (!$field instanceof FieldInterface) {
            throw new Exception("Field class \"{$fieldClass}\" must implements ".FieldInterface::class);
        }

        if ($field instanceof TranslatorAwareFieldInterface) {
            $field->setTranslator($this->translator);
        }
        if ($field instanceof TwigAwareFieldInterface) {
            $field->setTwig($this->twig);
        }

        return $field;
    }
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;
    }
1059377
public function export($dir, $path, $name, $type = 'file'){
    if ( ($cfg = $this->dir($dir)) &&
      ($root = $this->get_root_path($dir)) &&
      \defined('BBN_USER_PATH')
    ){
      $is_file = $type === 'file';
      $wtype = $is_file ? 'file' : 'directory';
      $rnd = bbn\str::genpwd();
      $root_dest = BBN_USER_PATH . 'tmp/' . $rnd . '/';
      $files = [];
      if ( !empty($cfg['tabs']) ){
        $root_dest_mvc = $root_dest . $name . '/mvc/';
        foreach ( $cfg['tabs'] as $t ){
          if ( empty($t['fixed']) ){
            $real = $t['path'];
            if ( dirname($path) !== '.' ){
              $real .= dirname($path) . '/';
            }
            if ( $is_file ){
              foreach ( $t['extensions'] as $e ){
                if ( is_file($root . $real . $name . '.' . $e['ext']) ){
                  $real .= $name . '.' . $e['ext'];
                  array_push($files, [
                    'src' => $root . $real,
                    'dest' => $root_dest_mvc . $real,
                    'is_file' => $is_file
                  ]);
                  break;
                }
              }
            }
            else {
              $real .= $name;
              if ( is_dir($root . $real) ){
                array_push($files, [
                  'src' =>$root . $real,
                  'dest' => $root_dest_mvc . $real,
                  'is_file' => $is_file
                ]);
              }
            }
          }
        }
      }
      else {
        if ( file_exists($root . $path) ){
          array_push($files, [
            'src' => $root . $path,
            'dest' => $root_dest . $path,
            'is_file' => $is_file
          ]);
        }
      }
      foreach ( $files as $f ){
        if ( $f['is_file'] ){
          if ( !bbn\file\dir::create_path(dirname($f['dest'])) ){
            $this->error("Impossible to create the path " . dirname($f['dest']));
            return false;
          }
        }
        if ( !bbn\file\dir::copy($f['src'], $f['dest']) ){
          $this->error('Impossible to export the ' . $wtype . ' ' . $f['src']);
          return false;
        }
      }

      if ( class_exists('\\ZipArchive') ){
        $filezip = BBN_USER_PATH.'tmp/'.$name.'.zip';
        $zip = new \ZipArchive();
        if ( $err = $zip->open($filezip, \ZipArchive::OVERWRITE) ){
          if ( file_exists($root_dest) ){
            if ( (!$is_file) || !empty($cfg['tabs']) ){
              // Create recursive directory iterator
              $files = bbn\file\dir::scan($root_dest);
              foreach ($files as $file){
                $tmp_dest = str_replace(
                  $root_dest . (empty($cfg['tabs']) ? '/' : ''),
                  (!empty($cfg['tabs']) ? 'mvc/' : ''),
                  $file
                );
                // Add current file to archive
                if ( ($file !== $root_dest.$name) &&
                  is_file($file) &&
                  !$zip->addFile($file, $tmp_dest)
                ){
                  $this->error("Impossible to add $file");
                  return false;
                }
              }
            }
            else {
              if ( !$zip->addFile($root_dest, $path) ){
                $this->error("Impossible to add $root_dest");
                return false;
              }
            }
            if ( $zip->close() ){
              if ( !bbn\file\dir::delete(BBN_USER_PATH . 'tmp/' . $rnd, 1) ){
                $this->error("Impossible to delete the directory " . BBN_USER_PATH . 'tmp/' . $rnd);
                return false;
              }
              return $filezip;
            }
            $this->error("Impossible to close the zip file $filezip");
            return false;
          }
          $this->error("The path does not exist: $root_dest");
          return false;
        }
        $this->error("Impossible to create $filezip ($err)");
        return false;
      }

      $this->error("ZipArchive class non-existent");
      return false;
    }
  }
1358932
public function createContentTypeWithRequiredFieldType( $fieldType, $name = null )
    {
        return $this->getFieldTypeManager()->createField( $fieldType, $name, true );
    }
269285
public function upload($post, $handler, $path = null)
    {
        $result = $this->transferred = null;
        $this->hook->attach('file.upload.before', $post, $handler, $path, $result, $this);

        if (isset($result)) {
            return $result;
        }

        if (!empty($post['error']) || empty($post['tmp_name']) || !is_uploaded_file($post['tmp_name'])) {
            return $this->translation->text('Unable to upload the file');
        }

        $this->setHandler($handler);
        $this->setDestination($path);

        $result = $this->validate($post['tmp_name'], $post['name']);

        if ($result !== true) {
            unlink($post['tmp_name']);
            return $result;
        }

        try {
            $result = $this->finalize($post['tmp_name'], $post['name'], true);
        } catch (Exception $ex) {
            $result = $ex->getMessage();
        }

        $this->hook->attach('file.upload.after', $post, $handler, $path, $result, $this);
        return $result;
    }
434479
protected function uploadMedia($type, $path, array $form = [])
    {
        if (!file_exists($path) || !is_readable($path)) {
            throw new InvalidArgumentException("File does not exist, or the file is unreadable: '$path'");
        }
        $form['type'] = $type;
        return $this->upload($this->getAPIByType($type), ['media' => $path], $form);
    }
1229705
protected function setDataType(Field & $field, $type)
    {
        $map = $this->dataTypeMap();

        if (array_key_exists($type, $map)) {
            $field->dataType = $map[$type];
        }

        return $this;
    }
1178254
protected function _store($uploadType, $name, $bytes, $overwrite = false) {
        if (!array_key_exists($uploadType, $this->_fileHandlers)) {
            $this->_fileHandlers[$uploadType] = $uploadType === Garp_File::TYPE_IMAGES ?
                new Garp_Image_File() :
                new Garp_File($uploadType);
        }
        return $this->_fileHandlers[$uploadType]->store($name, $bytes, $overwrite);
    }
82494
public function setFieldType($name, $type)
    {
        $this->checkFieldExist($name)->types[$name] = $type;

        return $this;
    }
1245206
public static function uploadFiles($field, $title, $action, array $value = [])
    {
        return self::upload($field, $title, $action, $value, Upload::TYPE_FILE);
    }
1339899
public function upload($name)
    {
        $instance = $this->_getInstance($name);
        require_once Plugin::classPath('Field') . 'Lib/class.upload.php';
        $uploader = new \upload($this->request->data['Filedata']);

        if (!empty($instance->settings['extensions'])) {
            $exts = explode(',', $instance->settings['extensions']);
            $exts = array_map('trim', $exts);
            $exts = array_map('strtolower', $exts);

            if (!in_array(strtolower($uploader->file_src_name_ext), $exts)) {
                $this->_error(__d('field', 'Invalid file extension.'), 501);
            }
        }

        $response = '';
        $uploader->file_overwrite = false;
        $folder = normalizePath(WWW_ROOT . "/files/{$instance->settings['upload_folder']}/");
        $url = normalizePath("/files/{$instance->settings['upload_folder']}/", '/');

        $uploader->process($folder);
        if ($uploader->processed) {
            $response = json_encode([
                'file_url' => Router::url($url . $uploader->file_dst_name, true),
                'file_size' => FileToolbox::bytesToSize($uploader->file_src_size),
                'file_name' => $uploader->file_dst_name,
                'mime_icon' => FileToolbox::fileIcon($uploader->file_src_mime),
            ]);
        } else {
            $this->_error(__d('field', 'File upload error, details: {0}', $uploader->error), 502);
        }

        $this->viewBuilder()->layout('ajax');
        $this->title(__d('field', 'Upload File'));
        $this->set(compact('response'));
    }