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 a...
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!

 
  1. <?php
  2. /**
  3. * DivulgacaoForm Form
  4. * @author <your name here>
  5. */
  6. class DivulgacaoForm extends TPage
  7. {
  8. protected $form; // form
  9. private $frame;
  10. /**
  11. * Form constructor
  12. * @param $param Request
  13. */
  14. public function __construct( $param )
  15. {
  16. parent::__construct();
  17. // creates the form
  18. $this->form = new TQuickForm('form_Divulgacao');
  19. $this->form->class = 'tform'; // change CSS class
  20. $this->form = new BootstrapFormWrapper($this->form);
  21. $this->form->style = 'display: table;width:100%'; // change style
  22. // define the form title
  23. $this->form->setFormTitle('Divulgacao');
  24. // create the form fields
  25. $id = new TEntry('id');
  26. $titulo_divulgacao = new TEntry('titulo_divulgacao');
  27. $data_validade = new TDate('data_validade');
  28. $caminho_imagem = new TFile('caminho_imagem');
  29. // complete upload action
  30. $caminho_imagem->setCompleteAction(new TAction(array($this, 'onComplete')));
  31. // add the fields
  32. $this->form->addQuickField('Id', $id, '30%' );
  33. $this->form->addQuickField('Titulo Divulgacao', $titulo_divulgacao, '70%' , new TRequiredValidator);
  34. $this->form->addQuickField('Data Validade', $data_validade, '70%' , new TRequiredValidator);
  35. $this->form->addQuickField('Caminho Imagem', $caminho_imagem, '70%' );
  36. $this->frame = new TElement('div');
  37. $this->frame->id = 'photo_frame';
  38. $this->frame->style = 'width:400px;height:auto;min-height:200px;border:1px solid gray;padding:4px;';
  39. $row = $this->form->addRow();
  40. $row->addCell('');
  41. $row->addCell($this->frame);
  42. if (!empty($id))
  43. {
  44. $id->setEditable(FALSE);
  45. }
  46. /** samples
  47. $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
  48. $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
  49. $fieldX->setSize( 100, 40 ); // set size
  50. **/
  51. // create the form actions
  52. $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
  53. $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onClear')), 'bs:plus-sign green');
  54. // vertical box container
  55. $container = new TVBox;
  56. $container->style = 'width: 90%';
  57. // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  58. $container->add(TPanelGroup::pack('Cadastro de Divulgacao', $this->form));
  59. $container->add(TPanelGroup::pack('', $this->frame));
  60. parent::add($container);
  61. }
  62. /**
  63. * Save form data
  64. * @param $param Request
  65. */
  66. public static function onComplete($param)
  67. {
  68. new TMessage('info', 'Upload completed: '.$param['caminho_imagem']);
  69. // refresh photo_frame
  70. TScript::create("$('#photo_frame').html('')");
  71. TScript::create("$('#photo_frame').append(\"<img style='width:100%' src='tmp/{$param['caminho_imagem']}'>\");");
  72. }
  73. public function onSave( $param )
  74. {
  75. try
  76. {
  77. TTransaction::open('dbarma'); // open a transaction
  78. /**
  79. // Enable Debug logger for SQL operations inside the transaction
  80. TTransaction::setLogger(new TLoggerSTD); // standard output
  81. TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  82. **/
  83. $this->form->validate(); // validate form data
  84. $object = new Divulgacao; // create an empty object
  85. $data = $this->form->getData(); // get form data as array
  86. $object->fromArray( (array) $data); // load the object with data
  87. $object->store(); // save the object
  88. // get the generated id
  89. $data->id = $object->id;
  90. $this->form->setData($data); // fill form data
  91. TTransaction::close(); // close the transaction
  92. if ($object instanceof Divulgacao)
  93. {
  94. $source_file = 'tmp/'.$object->caminho_imagem;
  95. $target_file = 'images/' . $object->caminho_imagem;
  96. $finfo = new finfo(FILEINFO_MIME_TYPE);
  97. // if the user uploaded a source file
  98. if (file_exists($source_file) AND ($finfo->file($source_file) == 'image/png' OR $finfo->file($source_file) == 'image/jpeg'))
  99. {
  100. // move to the target directory
  101. rename($source_file, $target_file);
  102. try
  103. {
  104. TTransaction::open($this->database);
  105. // update the photo_path
  106. $object->caminho_imagem = 'images/'.$object->caminho_imagem;
  107. $object->store();
  108. TTransaction::close();
  109. }
  110. catch (Exception $e) // in case of exception
  111. {
  112. new TMessage('error', $e->getMessage());
  113. TTransaction::rollback();
  114. }
  115. }
  116. $image = new TImage($object->caminho_imagem);
  117. $image->style = 'width: 100%';
  118. $this->frame->add( $image );
  119. }
  120. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  121. }
  122. catch (Exception $e) // in case of exception
  123. {
  124. new TMessage('error', $e->getMessage()); // shows the exception error message
  125. $this->form->setData( $this->form->getData() ); // keep form data
  126. TTransaction::rollback(); // undo all pending operations
  127. }
  128. }
  129. /**
  130. * Clear form data
  131. * @param $param Request
  132. */
  133. public function onClear( $param )
  134. {
  135. $this->form->clear(TRUE);
  136. }
  137. /**
  138. * Load object to form data
  139. * @param $param Request
  140. */
  141. public function onEdit( $param )
  142. {
  143. try
  144. {
  145. if (isset($param['key']))
  146. {
  147. $key = $param['key']; // get the parameter $key
  148. TTransaction::open('dbarma'); // open a transaction
  149. $object = new Divulgacao($key); // instantiates the Active Record
  150. $this->form->setData($object); // fill the form
  151. TTransaction::close(); // close the transaction
  152. }
  153. else
  154. {
  155. $this->form->clear(TRUE);
  156. }
  157. if (isset($param['key']))
  158. {
  159. $key = $param['key'];
  160. $object = new Divulgacao($key);
  161. if ($object)
  162. {
  163. $image = new TImage($object->caminho_imagem);
  164. $image->style = 'width: 100%';
  165. $this->frame->add( $image );
  166. }
  167. }
  168. }
  169. catch (Exception $e) // in case of exception
  170. {
  171. new TMessage('error', $e->getMessage()); // shows the exception error message
  172. TTransaction::rollback(); // undo all pending operations
  173. }
  174. }
  175. }
  176. ?>

Curso Dominando o Adianti Framework

O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!


Dominando o Adianti Framework Quero me inscrever agora!

Comentários (6)


NR

https://www.adianti.com.br/forum/pt/view_2315?erro-ao-gravar-imagem
SS

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?

NR

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.
SS

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
SS

Mais um detalhe. A foto não carrega na imagem de jeito nenhum. Mesmo informando upload completo.
SS

Pessoal, estou encerrando esse post e abrindo outro. Os erros já corrigi. O problema apresentado agora é outro.