Cache de imagem no upload Estou fazendo um formulário com imagem com base no tutor - ProductForm, porem estou renomeando a foto ao salvar. o formulario está renomeando a foto corretamente e salvando no banco de dados corretamente, porem ao mudar a foto (ele aparece correta), mas quando clico salvar, volta a foto antiga, embora na pasta ja esta a foto nova, no banco tambem fica correto, tenho que dar um f5 para atualizar...
LJ
Cache de imagem no upload  
Fechado
Estou fazendo um formulário com imagem com base no tutor - ProductForm, porem estou renomeando a foto ao salvar.
o formulario está renomeando a foto corretamente e salvando no banco de dados corretamente, porem ao mudar a foto (ele aparece correta), mas quando clico salvar, volta a foto antiga, embora na pasta ja esta a foto nova, no banco tambem fica correto, tenho que dar um f5 para atualizar. como evitar este f5.

 
  1. <?php
  2. /**
  3. * CategoriaForm Registration
  4. */
  5. class CategoriaForm extends TStandardForm
  6. {
  7. protected $form; // form
  8. private $frame;
  9. /**
  10. * Class constructor
  11. * Creates the page and the registration form
  12. */
  13. function __construct()
  14. {
  15. parent::__construct();
  16. Usuario::checkLogin();
  17. // creates the form
  18. $this->form = new TQuickForm('form_Categoria');
  19. $this->form->class = 'tform'; // CSS class
  20. // define the form title
  21. $this->form->setFormTitle('Categoria');
  22. // defines the database
  23. parent::setDatabase('sample');
  24. // defines the active record
  25. parent::setActiveRecord('Categoria');
  26. // create the form fields
  27. 1914 = new TEntry('id');
  28. $nome = new TEntry('nome');
  29. $dataevento = new TDate('dataevento');
  30. $descricao = new THtmlEditor('descricao');
  31. $imagem = new TFile('imagem');
  32. // add the fields
  33. $this->form->addQuickField('id', 1914, 50);
  34. $this->form->addQuickField('data', $dataevento, 100);
  35. $this->form->addQuickField('nome', $nome, 200);
  36. $this->form->addQuickField('descricao', $descricao, 200);
  37. $this->form->addQuickField('imagem', $imagem, 200);
  38. // complete upload action
  39. $imagem->setCompleteAction(new TAction(array($this, 'onComplete')));
  40. 1914->setEditable( FALSE );
  41. $descricao->setSize(400, 300);
  42. $this->frame = new TElement('div');
  43. $this->frame->id = 'photo_frame';
  44. $this->frame->style = 'width:400px;height:auto;min-height:200px;border:1px solid gray;padding:4px;';
  45. $row = $this->form->addRow();
  46. $row->addCell('');
  47. $row->addCell($this->frame);
  48. $imagem->setSize(200, 40);
  49. // add a form action
  50. $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'ico_save.png');
  51. $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onEdit')), 'ico_new.png');
  52. $this->form->addQuickAction(_t('Back'), new TAction(array('CategoriaList', 'onReload')), 'ico_back.png');
  53. // add the form to the page
  54. parent::add($this->form);
  55. }
  56. /**
  57. * On complete upload
  58. */
  59. public static function onComplete($param)
  60. {
  61. new TMessage('info', 'Upload completo: '.$param['imagem']);
  62. // refresh photo_frame
  63. TScript::create("$('#photo_frame').html('')");
  64. TScript::create("$('#photo_frame').append(\"<img style='width:100%' src='tmp/{$param['imagem']}'>\");");
  65. }
  66. /**
  67. * Edit product
  68. */
  69. public function onEdit($param)
  70. {
  71. $object = parent::onEdit($param);
  72. if ($object)
  73. {
  74. $imagem = new TImage($object->imagem);
  75. $imagem->style = 'width: 100%';
  76. $this->frame->add( $imagem );
  77. }
  78. }
  79. /**
  80. * Overloaded method onSave()
  81. * Executed whenever the user clicks at the save button
  82. */
  83. public function onSave()
  84. {
  85. // first, use the default onSave()
  86. $object = parent::onSave();
  87. // if the object has been saved
  88. if ($object instanceof Categoria)
  89. {
  90. $source_file = 'tmp/'.$object->imagem;
  91. $target_file = 'app/images/eventos/cat_' . $object->id.substr($object->imagem, -4);
  92. $finfo = new finfo(FILEINFO_MIME_TYPE);
  93. // if the user uploaded a source file
  94. if (file_exists($source_file) AND ($finfo->file($source_file) == 'image/png' OR $finfo->file($source_file) == 'image/jpeg'))
  95. {
  96. // move to the target directory
  97. rename($source_file, $target_file);
  98. try
  99. {
  100. TTransaction::open($this->database);
  101. // update the photo_path
  102. $object->imagem = $target_file;
  103. $object->store();
  104. TTransaction::close();
  105. }
  106. catch (Exception $e) // in case of exception
  107. {
  108. new TMessage('error', $e->getMessage());
  109. TTransaction::rollback();
  110. }
  111. }
  112. $image = new TImage($target_file);
  113. $image->style = 'width: 100%';
  114. $this->frame->add($image);
  115. // erro - só atualiza a imagem se der f5
  116. }
  117. }
  118. }
  119. ?>

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)


ES

No Final do onSave acrescente a linha:
 
  1. <?php $this->onReload(func_get_arg(0)); ?>
LJ

Olá Eliezer, obrigado por esta tentativa, mas não funcionou.
LJ

da esse erro:
Fatal error: Call to undefined method CategoriaForm::onReload() in C:wampwwwsuafoto2appcontrolCategoriaForm.class.php on line 145
ES

Significa que vc não definiu esse método e precisa criá-lo para carregar os dados chamar o método inicial que carrega os dados.
ES

Significa que vc não definiu esse método e precisa criá-lo para carregar os dados chamar o método inicial que carrega os dados.
PD

Às vezes pode ser cache do navegador simplesmente.
Quando é usado o mesmo nome de imagem no lado do servidor...