2011-03-28 57 views
2

我试图在CakePHP 1.3中使用MeioUpload behaviour来实现一些相当简单的图片上传,但我无法让我的生活得到它的工作。当我尝试在我的控制器中保存$this->data时,它会尝试保存常规文件阵列(缺少更好的单词),而不仅仅是文件名。在CakePHP 1.3中使用MeioUpload

下面是我在做什么:

我已经把meio_upload.php成/应用/型号/行为

在我的模型,我做了以下内容:

var $actsAs = array(
    'MeioUpload.MeioUpload' => array(
     'filename' => array(
      'dir' => 'img{DS}upload{DS}brawlers', 
      'allowedMime' => array('image/png'), 
      'allowedExt' => array('.png', '.PNG'), 
      'zoomCrop' => false, 
      'thumbsizes' => array(
       'normal' => array(
        'width' => 150, 
        'height' => 150 
       ) 
      ), 
      'default' => 'default.png', 
      'length' => array(
       'minWidth' => 100, 
       'minHeight' => 100, 
       'maxWidth' => 150, 
       'maxHeight' => 150 
      ) 
     )     
    )  
); 

在我看来,我有以下形式:

<?php 
echo $this->Form->create('Brawler', array('type' => 'file')); 
echo $this->Form->input('name', array(
             'label' => 'Name', 
             'maxLength' => '45' 
            ) 
         ); 
echo $this->Form->input('comment', array(
             'label' => 'Description', 
             'rows' => '3' 
             ) 
         ); 
echo $this->Form->input('author', array(
             'label' => 'Your name)', 
             'maxLength' => '45' 
             ) 
         ); 
echo $this->Form->input('email', array(
             'label' => 'Email (will not be shown)', 
             'maxLength' => '45' 
             ) 
         ); 
echo $this->Form->input(
         'filename', 
         array(
           'between'=>'<br />', 
           'type'=>'file', 
           'label' => 'Image (Max 2mb, 150x150 pixels, .png)' 
          ) 
         ); 
echo $this->Form->end('Submit'); 
?> 

最后我在关联的控件中添加动作呃看起来是这样的:

function add() { 
    if (!empty($this->data)) { 
     $this->Brawler->create(); 

     if($this->Brawler->save($this->data)) { 
      $this->Session->setFlash('The brawler has been saved', true); 
      $this->redirect(array('action'=>'index')); 
     } else { 
      $this->Session->setFlash('The Brawler could not be saved. Please try again.', true); 
      debug($this->data); 
      debug($this->validationErrors); 
      die(); 
      //$this->redirect(array('action'=>'add')); 
     } 
    } 
} 

对于后人,这里是我的表设计:

delimiter $$ 

CREATE TABLE `brawlers` (
    `id` int(11) NOT NULL, 
    `name` varchar(45) NOT NULL, 
    `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    `comment` text, 
    `email` varchar(45) NOT NULL, 
    `author` varchar(45) NOT NULL, 
    `filename` varchar(100) NOT NULL, 
    PRIMARY KEY (`id`), 
) ENGINE=MyISAM DEFAULT CHARSET=utf8$$ 

当我尝试提交我的形式,这是输出我得到:

app/controllers/brawlers_controller.php (line 37) 
Array 
(
    [Brawler] => Array 
     (
      [name] => Viking 
      [comment] => Herp. This is a description. 
      [author] => Me 
      [email] => [email protected] 
      [filename] => Array 
       (
        [name] => 5.png 
        [type] => image/png 
        [tmp_name] => /storage/configuration/upload_tmp_dir/phpEF2okD 
        [error] => 0 
        [size] => 15863 
       ) 

     ) 

) 

app/controllers/brawlers_controller.php (line 37) 

显然,当它试图将数组保存到文件名字段时失败。图像从不保存在指定的上传目录中。似乎meioupload行为从未实际使用过。我怎样才能验证这一点?

你不得不原谅我发布的大量代码,但我认为我告诉你所有的一切,而不是猜测我可能在做什么。如果有人能够发现错误,那可以为我节省很多小时的头发。

回答

1

嘿,我有答案UR查询

也就是说u必须使花药可变说

$ d1和复制$这个 - >日期数组该数组像下面

$ D1 [ 'brawlers'] ['name'] = $ this-> data ['brawlers'] ['name'];

所有乌尔变量

$ D1 [ '捣乱'] [ '文件名'] = $这 - >数据[ '捣乱'] [ '文件名'] [ '名称'];

然后保存它像$ this-> brawlers-> save($ d1);

0

我是不是 PHP的家伙,但我不得不使用CakePHP和Meio.Upload前一段时间。 AFAIK你需要在数据库中4个领域的图像:

  • 文件名(你可以改变这一个在 设置的名称)
  • DIR
  • MIME类型
  • 文件大小

来看在你的错误和数据库模式,我会说你错过了一些领域。

编辑:documentation

0

我有同样的问题,您正在使用的MeioUpload版本是否与你的蛋糕的版本不兼容。我结束使用MeioUpload的2.3版本。