Monday, February 6, 2012

strange image upload editing behavior

Hi everybody, I'm working on creating an image upload section for my
site, and creating the images seems to work fine. The uploaded image
information is saved correctly, the image is moved to the correct
folder and it looks like I can call the image from other parts of the
site just fine.

My problem comes up when I'm trying to edit the image. When I upload
a new file, it appears that it only takes the first letter as the name
for the file. For example, if I create an image on my site for the
google logo called "google.png", it will upload fine, but if
afterwards I try to upload a different file named "google.png" it will
call it just "g". or if I try to upload a file called "backup.png" it
will take the name as just "b".

It's not too big a problem since I can just delete the image and
create a new record for one, but it would be nice to be able to edit
the image and keep the rest of the record intact since I record some
other information along with what the image name is.

Here's the code from the add.ctp and edit.ctp files:

add ---
<?php
echo $this->Form->create('imagelink', array('type'=>'file'));
echo $this->Form->input('name');
echo $this->Form->input('type');
echo "Link"; echo $this->Form->text('link');
echo $this->Form->input('place');

echo "Info"; echo $this->Form->file('info');

echo $this->Form->end('Save Element');
?>
edit ---
<?php
echo $this->Form->create('imagelink',array('action'=>'edit'));
echo $this->Form->input('name');
echo $this->Form->input('type');
echo "Link"; echo $this->Form->text('link');
echo $this->Form->input('place');

echo "Info"; echo $this->Form->file('info');

echo $this->Form->input('id',array('type'=>'hidden'));
echo $this->Form->end('Save Modifications');
?>

The controller has...

public function add(){
if ($this->request->is('post')){
if ($this->imagelink->save($this->request->data)){
$this->Session->setFlash('New element has been saved.');
$this->redirect(array('action'=>'index'));
} else {
$this->Session ->setFlash('Unable to add new element.');
}
}
}

public function edit($id = null){
$this->imagelink->id = $id;
if ($this->request->is('get')) {
$this->request->data = $this->imagelink->read();
} else {
if ($this->imagelink->save($this->request->data)) {
$this->Session->setFlash('Element has been updated.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to update the
element.');
}
}
}

.. and the model has the beforeSave method:

move_uploaded_file($this->data['imagelink']['info']['tmp_name'],'img/'.
$this->data['imagelink']['info']['name']);
$this->data['imagelink']['info'] = $this->data['imagelink']['info']
['name'];
return true;

The database is only saving the image name instead of taking the whole
image as a blob, so before saving I wanted the image itself to be
moved to the img folder, and the name of the image to be stored in the
database.

If anybody can see anything that would cause cake to only take the
first letter of the new uploaded image when editing the record, or if
you suspect something, please let me know.

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: