1

CakePHP 2.1を使用していて、サーバーにファイルをアップロードしようとしています。

手動でファイルに名前を付けることで、指定したディレクトリにファイルを正常に移動できますが、ビューのファイルタイプから名前を取得していません:register。

データベーステーブル:ユーザー

Id Auto_Increment
username
file_name

コントローラー:UsersController.php

public function register(){
    if ($this->request->is('post')){
        //   $this->data['User']['resume_file']['tmp_name'] = 'test.php';
        //  The above statement is working good

        if ($this->User->save($this->request->data)){
            $this->Session->setFlash('Message : ' . $this->data['User']['resume_file']['tmp_name']);
            //debug($this->data['User']['resume_file']['tmp_name']);

            // This is giving an error, while inserting
        }
    }
}

表示:users / register.php

<?php
    echo $this->Form->create('User', array('type'=>'file'));
    echo $this->Form->input('username');
    echo $this->Form->input('file_name', array('type'=>'file', 'label'=>'Upload document') );
    echo $this->Form->end('Register');
?>

エラー:データベースエラー

INSERT INTO `sample_db`.`users` (`file_name`, `created`) VALUES (Array, '2012-06-14 08:10:10')

私が見たのは、テーブルに挿入しているときに、CakePHPにその値Arrayとエラー Array to string conversionエラーが挿入されていることです。CakePHPでその値を取得するにはどうすればよいですか。input type : file_name

または、以下のリクエストステートメントで保存する前にデータを変更できる場合。

$this->request->data

私はそれを変えようとしました、以下のステートメントで

$this->data['User']['resume_file']['tmp_name'] = 'test.php';

しかし、同じエラーを返すことはできませんでした。

4

1 に答える 1

3

これを試して:

public function register(){
    if ($this->request->is('post')){
        $filename = $this->request->data['User']['file_name']['tmp_name'];
        $this->data['User']['file_name'] = $filename;

        if ($this->User->save($this->request->data)){
            $this->Session->setFlash('Message : ' . $filename);
        }
    }
}
于 2012-06-14T08:37:48.343 に答える