为什么这个上传文件代码在CakePHP中不起作用

时间:2012-08-16 14:38:20

标签: php mysql cakephp

我在CakePHP 2.1中上传文件时遇到问题。事实上,我总是有错误:

Column not found: 1054 Unknown column 'Array' in 'field list'.

视图:

<?php echo $this->Form->create('Ecole',array('enctype' => 'multipart/form-data')); ?>
<?php echo $this->Form->input('Ecole.logo_ecole', array('type'=>'file','class'=>'','label'=>'')); ?>

当我删除array('enctype' => 'multipart/form-data')时,我没有错误,但上传也无效。

对于控制器:

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'));
  }
}

任何人都可以解释我做错了什么以及如何做对吗?

3 个答案:

答案 0 :(得分:2)

要执行multipart / form-data,您必须使用帮助程序

以这种方式指定它
 <?php echo $this->Form->create('Ecole', array('type' => 'file')); ?>

类型可以是‘post’, ‘get’, ‘file’, ‘put’ or ‘delete’。请参阅FormHelper文档中的Options for create部分!

答案 1 :(得分:0)

这可能是因为您在上传文件时尝试保存阵列蛋糕($this->data['Ecole']['logo_ecole']是一个数组)。您是否只想将文件名保存到数据库中?

答案 2 :(得分:0)

我修改了你的代码请看看

请不要在表单

中删除array('enctype' => 'multipart/form-data')这一行
<?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'));
      }

}