1

I am learning CakePHP 2.0, and created a sample test application, where user can submit a file at the time of registration.

below is the database table

users Table

id Auto_Increment
first_name
last_name
email
doc_file

And also created User.php and UsersController.php

and below are the content code in UsersController.php

UsersController.php

class UsersController extends AppController{

    public $helpers = array('Html', 'Form');


    public function index(){
        $this->set('user1', $this->User->find('all'));
    }


    public function register(){
        if ($this->request->is('post')){
            if ($this->User->save($this->request->data)){
                //move_uploaded_file($this->data['Model']['field']['tmp_name'], WWW_ROOT.DS.'xxx');

                move_uploaded_file($this->data['User']['doc_file']['tmp_name'], WWW_ROOT.DS.'hello.doc');

                $this->Session->setFlash('User is created');
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash('Cannot register a user');
            }
        }
    }
}

And in Views, I have created a two files ie index.ctp and register.ctp with a directory Users in View directory

code content of register.ctp

register.ctp

<?php

    echo $this->Form->create('User', array('type'=>'file'));
    echo $this->Form->input('first_name', array('label'=>'First Name'));
    echo $this->Form->input('last_name', array('label'=>'Last Name'));
    echo $this->Form->input('email');
    echo $this->Form->input('doc_file', array('type'=>'file'));
    echo $this->Form->end('Register');
?>  

And when run this page, and fill up all information, it gives an error

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'

And below is the query I got, where it is inserting an array into doc_file

SQL Query: INSERT INTO `database_db`.`users` (`first_name`, `last_name`, `email`, `doc_file`) VALUES ('master', 'shifu', '[email protected]', Array)

What i am trying :

While user is registering, the file name should be random and it should be move to

localhost/mysite/app/webroot/files/user_data/ or localhost/mysite/app/webroot/files/user_data/user_id_directory/

its good, if it creates the userid directory and storing the file in its parent user directory

2 Answers 2

2

The problem is that CakePHP is seeing the doc_file field and trying to insert it into the database.

After Cake has processed the form submission, doc_file contains an array of values.

You can fix it by doing the following:

In your view:

echo $this->Form->input('new_doc_file', array('type'=>'file'));

In your controller:

if ($this->request->is('post')){
    $this->data['User']['doc_file'] = WWW_ROOT.DS.'hello.doc';
    if ($this->User->save($this->request->data)){
        move_uploaded_file($this->data['User']['new_doc_file']['tmp_name'], $this->data['User']['doc_file']);

        $this->Session->setFlash('User is created');
        $this->redirect(array('action'=>'index'));
    } else {
        $this->Session->setFlash('Cannot register a user');
    }
}

(ie. upload the file with a temporary field name, manually add the doc_file field to the post array before you try to save).

Sign up to request clarification or add additional context in comments.

4 Comments

see this statement $this->User->save($this->request->data) is storing values into database... I got the uploading of file , but how can the name be inserted into database table. And when saving, its giving an error ie Database Error ie an Array cannot be inserted into table field right.. We need a single value from that Array.
@RicharAtHome can you let me know, how to manually add the doc_file field to the post array before you try to save
I'm manually adding the doc_file field to the post array with this line: $this->data['User']['doc_file'] = WWW_ROOT.DS.'hello.doc';
Then use some simple regex or string slicing to extract the name from the new_doc_file field :-)
0

The error suggests that

$this->data['User']['doc_file']['tmp_name']

is returning an array - try

debug($this->data['User']['doc_file']['tmp_name']) 

in your controller code to see what it contains.

1 Comment

Its returning something like this D:\xampp\tmp\phpF639.tmp and \app\Controller\UsersController.php (line 19)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.