2011-03-28 51 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版本。