SS
Problemas na salva de imagens - Upload de imagem no TFile
Pessoal, é o seguinte!
Estou fazendo uma implementação de acordo com os exemplos do Tutor.
Mas por algum motivo (ainda não descobri qual), a imagem não carrega para meu campo Image.
Ao tentar salvar o registro dá o seguinte erro:
Fatal error: Class 'finfo' not found in C:xampphtdocsctrlarmasappcontrolDivulgacaoForm.class.php on line 126
Em anexo, segue como fica a tela após carregar a imagem, e abaixo segue o código da pagina!
Estou fazendo uma implementação de acordo com os exemplos do Tutor.
Mas por algum motivo (ainda não descobri qual), a imagem não carrega para meu campo Image.
Ao tentar salvar o registro dá o seguinte erro:
Fatal error: Class 'finfo' not found in C:xampphtdocsctrlarmasappcontrolDivulgacaoForm.class.php on line 126
Em anexo, segue como fica a tela após carregar a imagem, e abaixo segue o código da pagina!
- <?php
- /**
- * DivulgacaoForm Form
- * @author <your name here>
- */
- class DivulgacaoForm extends TPage
- {
- protected $form; // form
- private $frame;
-
- /**
- * Form constructor
- * @param $param Request
- */
- public function __construct( $param )
- {
- parent::__construct();
-
- // creates the form
- $this->form = new TQuickForm('form_Divulgacao');
- $this->form->class = 'tform'; // change CSS class
- $this->form = new BootstrapFormWrapper($this->form);
- $this->form->style = 'display: table;width:100%'; // change style
-
- // define the form title
- $this->form->setFormTitle('Divulgacao');
-
- // create the form fields
- $id = new TEntry('id');
- $titulo_divulgacao = new TEntry('titulo_divulgacao');
- $data_validade = new TDate('data_validade');
- $caminho_imagem = new TFile('caminho_imagem');
-
- // complete upload action
- $caminho_imagem->setCompleteAction(new TAction(array($this, 'onComplete')));
- // add the fields
- $this->form->addQuickField('Id', $id, '30%' );
- $this->form->addQuickField('Titulo Divulgacao', $titulo_divulgacao, '70%' , new TRequiredValidator);
- $this->form->addQuickField('Data Validade', $data_validade, '70%' , new TRequiredValidator);
- $this->form->addQuickField('Caminho Imagem', $caminho_imagem, '70%' );
- $this->frame = new TElement('div');
- $this->frame->id = 'photo_frame';
- $this->frame->style = 'width:400px;height:auto;min-height:200px;border:1px solid gray;padding:4px;';
- $row = $this->form->addRow();
- $row->addCell('');
- $row->addCell($this->frame);
- if (!empty($id))
- {
- $id->setEditable(FALSE);
- }
-
- /** samples
- $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
- $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
- $fieldX->setSize( 100, 40 ); // set size
- **/
-
- // create the form actions
- $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
- $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onClear')), 'bs:plus-sign green');
-
- // vertical box container
- $container = new TVBox;
- $container->style = 'width: 90%';
- // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add(TPanelGroup::pack('Cadastro de Divulgacao', $this->form));
- $container->add(TPanelGroup::pack('', $this->frame));
-
- parent::add($container);
- }
- /**
- * Save form data
- * @param $param Request
- */
-
- public static function onComplete($param)
- {
- new TMessage('info', 'Upload completed: '.$param['caminho_imagem']);
-
- // refresh photo_frame
- TScript::create("$('#photo_frame').html('')");
- TScript::create("$('#photo_frame').append(\"<img style='width:100%' src='tmp/{$param['caminho_imagem']}'>\");");
- }
-
- public function onSave( $param )
- {
- try
- {
- TTransaction::open('dbarma'); // open a transaction
-
- /**
- // Enable Debug logger for SQL operations inside the transaction
- TTransaction::setLogger(new TLoggerSTD); // standard output
- TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
- **/
-
- $this->form->validate(); // validate form data
-
- $object = new Divulgacao; // create an empty object
- $data = $this->form->getData(); // get form data as array
- $object->fromArray( (array) $data); // load the object with data
- $object->store(); // save the object
-
- // get the generated id
- $data->id = $object->id;
-
- $this->form->setData($data); // fill form data
- TTransaction::close(); // close the transaction
- if ($object instanceof Divulgacao)
- {
- $source_file = 'tmp/'.$object->caminho_imagem;
- $target_file = 'images/' . $object->caminho_imagem;
- $finfo = new finfo(FILEINFO_MIME_TYPE);
-
- // if the user uploaded a source file
- if (file_exists($source_file) AND ($finfo->file($source_file) == 'image/png' OR $finfo->file($source_file) == 'image/jpeg'))
- {
- // move to the target directory
- rename($source_file, $target_file);
- try
- {
- TTransaction::open($this->database);
- // update the photo_path
- $object->caminho_imagem = 'images/'.$object->caminho_imagem;
- $object->store();
-
- TTransaction::close();
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage());
- TTransaction::rollback();
- }
- }
- $image = new TImage($object->caminho_imagem);
- $image->style = 'width: 100%';
- $this->frame->add( $image );
- }
-
- new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- $this->form->setData( $this->form->getData() ); // keep form data
- TTransaction::rollback(); // undo all pending operations
- }
- }
-
- /**
- * Clear form data
- * @param $param Request
- */
- public function onClear( $param )
- {
- $this->form->clear(TRUE);
- }
-
- /**
- * Load object to form data
- * @param $param Request
- */
- public function onEdit( $param )
- {
- try
- {
- if (isset($param['key']))
- {
- $key = $param['key']; // get the parameter $key
- TTransaction::open('dbarma'); // open a transaction
- $object = new Divulgacao($key); // instantiates the Active Record
- $this->form->setData($object); // fill the form
- TTransaction::close(); // close the transaction
- }
- else
- {
- $this->form->clear(TRUE);
- }
-
- if (isset($param['key']))
- {
- $key = $param['key'];
- $object = new Divulgacao($key);
-
- if ($object)
- {
- $image = new TImage($object->caminho_imagem);
- $image->style = 'width: 100%';
- $this->frame->add( $image );
- }
-
- }
-
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- TTransaction::rollback(); // undo all pending operations
- }
- }
- }
- ?>
https://www.adianti.com.br/forum/pt/view_2315?erro-ao-gravar-imagem
OK, Nataniel... vou ver esse parâmetro, mas isso explica por que a imagem não carrega para o componente após o upload, ou tem algo de errado no meu código?
Você chegou e ver a imagen do anexo?
Sérgio, não analisei detalhadamente o restante do código. Mas sim, explica o não carregamento da imagem, pois um "Fatal Error" interrompe a execução do script.
Pessoal, boa noite!
Habilitei a extensão e o erro mudou.
Uma coisa que percebi é que pediu usuario e senha na hora de carregar a imagem para o localhost. Coloquei a senha e usuario do computador mas não sei se é o correto.
Segue abaixo as mensagens de erro!
Erro
Arquivo não encontrado: '.ini'
Warning: rename(tmp/_DSC0443.jpg,images/_DSC0443.jpg): in C:xampphtdocsctrlarmasappcontrolDivulgacaoForm.class.php on line 132
Mais um detalhe. A foto não carrega na imagem de jeito nenhum. Mesmo informando upload completo.
Pessoal, estou encerrando esse post e abrindo outro. Os erros já corrigi. O problema apresentado agora é outro.