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.
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.
- <?php
- /**
- * CategoriaForm Registration
- */
- class CategoriaForm extends TStandardForm
- {
- protected $form; // form
- private $frame;
- /**
- * Class constructor
- * Creates the page and the registration form
- */
- function __construct()
- {
- parent::__construct();
- Usuario::checkLogin();
- // creates the form
- $this->form = new TQuickForm('form_Categoria');
- $this->form->class = 'tform'; // CSS class
- // define the form title
- $this->form->setFormTitle('Categoria');
- // defines the database
- parent::setDatabase('sample');
- // defines the active record
- parent::setActiveRecord('Categoria');
- // create the form fields
- 1914 = new TEntry('id');
- $nome = new TEntry('nome');
- $dataevento = new TDate('dataevento');
- $descricao = new THtmlEditor('descricao');
- $imagem = new TFile('imagem');
- // add the fields
- $this->form->addQuickField('id', 1914, 50);
- $this->form->addQuickField('data', $dataevento, 100);
- $this->form->addQuickField('nome', $nome, 200);
- $this->form->addQuickField('descricao', $descricao, 200);
- $this->form->addQuickField('imagem', $imagem, 200);
- // complete upload action
- $imagem->setCompleteAction(new TAction(array($this, 'onComplete')));
- 1914->setEditable( FALSE );
- $descricao->setSize(400, 300);
- $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);
- $imagem->setSize(200, 40);
- // add a form action
- $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'ico_save.png');
- $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onEdit')), 'ico_new.png');
- $this->form->addQuickAction(_t('Back'), new TAction(array('CategoriaList', 'onReload')), 'ico_back.png');
- // add the form to the page
- parent::add($this->form);
- }
- /**
- * On complete upload
- */
- public static function onComplete($param)
- {
- new TMessage('info', 'Upload completo: '.$param['imagem']);
- // refresh photo_frame
- TScript::create("$('#photo_frame').html('')");
- TScript::create("$('#photo_frame').append(\"<img style='width:100%' src='tmp/{$param['imagem']}'>\");");
- }
- /**
- * Edit product
- */
- public function onEdit($param)
- {
- $object = parent::onEdit($param);
- if ($object)
- {
- $imagem = new TImage($object->imagem);
- $imagem->style = 'width: 100%';
- $this->frame->add( $imagem );
- }
- }
- /**
- * Overloaded method onSave()
- * Executed whenever the user clicks at the save button
- */
- public function onSave()
- {
- // first, use the default onSave()
- $object = parent::onSave();
- // if the object has been saved
- if ($object instanceof Categoria)
- {
- $source_file = 'tmp/'.$object->imagem;
- $target_file = 'app/images/eventos/cat_' . $object->id.substr($object->imagem, -4);
- $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->imagem = $target_file;
- $object->store();
- TTransaction::close();
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage());
- TTransaction::rollback();
- }
- }
- $image = new TImage($target_file);
- $image->style = 'width: 100%';
- $this->frame->add($image);
- // erro - só atualiza a imagem se der f5
- }
- }
- }
- ?>
No Final do onSave acrescente a linha:
Olá Eliezer, obrigado por esta tentativa, mas não funcionou.
da esse erro:
Fatal error: Call to undefined method CategoriaForm::onReload() in C:wampwwwsuafoto2appcontrolCategoriaForm.class.php on line 145
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.
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.
Às vezes pode ser cache do navegador simplesmente.
Quando é usado o mesmo nome de imagem no lado do servidor...