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