【问题标题】:Array to string Error while uploading an Image上传图像时数组到字符串错误
【发布时间】:2014-10-07 10:50:27
【问题描述】:

我尝试使用 cakephp 上传图片,但出现以下错误:

注意(8):数组到字符串的转换[CORE\Cake\Model\Datasource\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');
?>

我所做的有什么问题吗?

【问题讨论】:

    标签: cakephp image-uploading


    【解决方案1】:

    你好好学习 cakephp 手册 HOW form type can be File ?????? :)

    使用这个

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

    【讨论】:

    • 谢谢,但仍然出现错误:无法确定 mimetype。
    • 这就是为什么我将它设置为文件类型,阅读评论:stackoverflow.com/questions/20770144/…
    • 它给了我完全相同的错误:无法确定 mimetype。
    • 我之前遇到过数组到字符串转换错误,我相信 'enctype'=>'multipart/form-data' 解决了这个问题,但我猜无论是否有 'file'=>' 都一样类型'
    • @jQueryAngryBird 'type' =&gt; 'file' 是定义表单类型的正确方法,也许应该更仔细地研究一下手册。 FormHelper > Options for create()
    【解决方案2】:

    您需要在控制器中处理文件上传。如果您调试请求,您会看到 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

    【讨论】:

      猜你喜欢
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 2013-10-05
      • 1970-01-01
      • 2022-11-25
      • 2016-06-11
      • 1970-01-01
      • 2013-04-27
      相关资源
      最近更新 更多