Reputation: 243
I have a problem with the upload of files in CakePHP 2.1. In fact, I always have the error:
Column not found: 1054 Unknown column 'Array' in 'field list'.
for the view:
<?php echo $this->Form->create('Ecole',array('enctype' => 'multipart/form-data')); ?>
<?php echo $this->Form->input('Ecole.logo_ecole', array('type'=>'file','class'=>'','label'=>'')); ?>
When I remove array('enctype' => 'multipart/form-data')
I don't have the error but the upload don't work either.
For the controller:
if(!empty($this->data))
{
debug($this->data);
$ext = 'jpg';
// Save success
if($this->Ecole->save($this->data))
{
// Destination folder, new filename and destination path
$dest_folder = IMAGES . DS . 'galleries' . DS . $this->Ecole->id;
$new_filename = $this->Ecole->id. '.' .$ext;
$dest_path = $dest_folder . DS . $new_filename;
// Check if destination folder exists and create if it doesn't
if(!is_dir($dest_folder))
{
mkdir($dest_folder, 0755, true);
}
// We move the picture and rename it with his id
if(move_uploaded_file($this->data['Ecole']['logo_ecole']['tmp_name'], $dest_path))
{
// Show success flash message
$this->Session->setFlash(__('Picture successfully added !', true), 'default', array('class' => 'success'));
echo "<script> parent.location.reload(true); parent.jQuery.fancybox.close(); </script>";
}
// Move failed
else
{
// Delete picture
//$this->Ecole->delete($this->Ecole->id);
// Show error flash message
$this->Session->setFlash(__('Error occurred while adding picture !', true), 'default', array('class' => 'error'));
}
}
// Save failed
else
{
// Show error flash message
$this->Session->setFlash(__('Error occurred while adding picture !', true), 'default', array('class' => 'error'));
}
}
Can anyone explain what I'm doing wrong and how to do it right?
Upvotes: 0
Views: 1462
Reputation: 7762
i have modify your code please take a look
and please not remove array('enctype' => 'multipart/form-data')
this line in form
<?php
if(!empty($this->data))
{
debug($this->data);
$ext = 'jpg';
// Destination folder, new filename and destination path
$dest_folder = IMAGES . DS . 'galleries' . DS . $this->Ecole->id;
$new_filename = $this->Ecole->id. '.' .$ext;
$dest_path = $dest_folder . DS . $new_filename;
// Check if destination folder exists and create if it doesn't
if(!is_dir($dest_folder))
{
mkdir($dest_folder, 0755, true);
}
$image='';
// We move the picture and rename it with his id
if(move_uploaded_file($this->data['Ecole']['logo_ecole']['tmp_name'], $dest_path))
{
$image = basename($this->data['Ecole']['logo_ecole']['name'])
// Show success flash message
$this->Session->setFlash(__('Picture successfully added !', true), 'default', array('class' => 'success'));
echo "<script> parent.location.reload(true); parent.jQuery.fancybox.close(); </script>";
}else
{
// Delete picture
//$this->Ecole->delete($this->Ecole->id);
// Show error flash message
$this->Session->setFlash(__('Error occurred while adding picture !', true), 'default', array('class' => 'error'));
}
$this->data['Ecole']['logo_ecole'] = $image;
// Save success
if(!$this->Ecole->save($this->data))
{
// Show error flash message
$this->Session->setFlash(__('Error occurred while adding picture !', true), 'default', array('class' => 'error'));
}
}
Upvotes: 0
Reputation: 1772
It's probably because you're trying to save the array cake generates when uploading a file ($this->data['Ecole']['logo_ecole']
is an array). Are you meaning to save only the filename to the database?
Upvotes: 0
Reputation: 8420
to do multipart/form-data, you have to specify it this way with the helper
<?php echo $this->Form->create('Ecole', array('type' => 'file')); ?>
The type can be ‘post’, ‘get’, ‘file’, ‘put’ or ‘delete’
. Please see the sections Options for create
here in the FormHelper documentation !
Upvotes: 2