php - 为什么此上传文件代码在 CakePHP 中不起作用

标签 php mysql cakephp

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

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

对于 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'=>'')); ?>

当我删除 array('enctype' => 'multipart/form-data') 时,我没有出现错误,但上传也不起作用。

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

谁能解释一下我做错了什么以及如何做正确的事情?

最佳答案

要执行 multipart/form-data,您必须使用帮助程序以这种方式指定它

 <?php echo $this->Form->create('Ecole', array('type' => 'file')); ?>

类型可以是‘post’、‘get’、‘file’、‘put’或‘delete’。请参阅 FormHelper 中的创建选项部分。文档!

关于php - 为什么此上传文件代码在 CakePHP 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11989598/

相关文章:

php - CakePHP 3 显示 DebugKit 面板

php - 为 MYSQL 中的 foreach() 提供的参数无效,适用于第一个表而不是第二个表

php - MySQL 在一个查询中进行多项选择

MySQL 触发器在特定行上更改第二个表

mysql - 如何将 2 个表中的信息放入一个 sql 查询中?

php - cakephp2.1 中的数据获取问题

php - 如何让 Zend Studio 自动完成与 CakePHP 模型一起工作?

php - 使用 PHP 列出服务器上所有正在运行的进程

php - xcode 在不需要时打印 &lt;meta&gt; 标签

php - 在php中输入卫生