2014-10-07 69 views
-2

我试图上载使用CakePHP图像,我得到以下错误:阵列串错误而上传图片

通知(8):Array对字符串的转换[CORE \蛋糕\模型\数据源\ DboSource。 php,line 1009]

<?php echo $this->Form->create('User',array('type'=>'file')); 
     echo $this->Form->input('profile_pic', array('type'=>'file')); 
     echo $this->Form->end('submit'); 
?> 

我做了什么不对?

回答

0

你研究的CakePHP手册妥善如何形成的类型可以是文件?????? :)

使用此

<?php echo $this->Form->create('User',array('enctype'=>'multipart/form-data')); 
     echo $this->Form->input('profile_pic', array('type'=>'file')); 
     echo $this->Form->end('submit'); 
?> 
+0

感谢,但仍得到一个错误: 无法确定MIME类型。 – Exchanger13 2014-10-08 08:15:05

+0

这就是为什么我把它设置为文件类型,阅读评论: http://stackoverflow.com/questions/20770144/cakephp-image-can-not-determine-the-mimetype – Exchanger13 2014-10-08 08:17:46

+0

您的项目是否与上述代码一起工作? – 2014-10-08 08:37:08

0

您需要处理控制器中的文件上传。如果调试请求你会看到profile_pic领域是一个数组:

# in controller: 
if ($this->request->is('post')) { 
      debug($this->request->data); die(); 
     } 

# result: 
array(
    'User' => array(
     'profile_pic' => array(
      'name' => 'wappy500x500.jpg', 
      'type' => 'image/jpeg', 
      'tmp_name' => '/tmp/phptk28hE', 
      'error' => (int) 0, 
      'size' => (int) 238264 
     ) 
    ) 
) 

简短的回答:

public function upload() { 
     if ($this->request->is('post')) { 
      if(isset($this->request->data['User']['profile_pic']['error']) && $this->request->data['User']['profile_pic']['error'] === 0) { 
       $source = $this->request->data['User']['profile_pic']['tmp_name']; // Source 
       $dest = ROOT . DS . 'app' . DS . 'webroot' . DS . 'uploads' . DS; // Destination 
       move_uploaded_file($source, $dest.'your-file-name.jpg'); // Move from source to destination (you need write permissions in that dir) 
       $this->request->data['User']['profile_pic'] = 'your-file-name.jpg'; // Replace the array with a string in order to save it in the DB 
       $this->User->create(); // We have a new entry 
       $this->User->save($this->request->data); // Save the request 
       $this->Session->setFlash(__('The user has been saved.')); // Send a success flash message 
      } else { 
       $this->Session->setFlash(__('The user could not be saved. Please, try again.')); 
      } 

     } 
    } 

当然,你需要在上传文件的额外验证。

延伸阅读:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:stackoverflow.com+cakephp+upload+file