Note that there are some explanatory texts on larger screens.

plurals
  1. POupload file in cakephp without pluggin or using controller code
    primarykey
    data
    text
    <p>I am learning CakePHP 2.0, and created a sample test application, where user can submit a file at the time of registration.</p> <p>below is the database table</p> <h2>users Table</h2> <pre><code>id Auto_Increment first_name last_name email doc_file </code></pre> <p>And also created <code>User.php</code> and <code>UsersController.php</code></p> <p>and below are the content code in <code>UsersController.php</code></p> <h2>UsersController.php</h2> <pre><code>class UsersController extends AppController{ public $helpers = array('Html', 'Form'); public function index(){ $this-&gt;set('user1', $this-&gt;User-&gt;find('all')); } public function register(){ if ($this-&gt;request-&gt;is('post')){ if ($this-&gt;User-&gt;save($this-&gt;request-&gt;data)){ //move_uploaded_file($this-&gt;data['Model']['field']['tmp_name'], WWW_ROOT.DS.'xxx'); move_uploaded_file($this-&gt;data['User']['doc_file']['tmp_name'], WWW_ROOT.DS.'hello.doc'); $this-&gt;Session-&gt;setFlash('User is created'); $this-&gt;redirect(array('action'=&gt;'index')); } else { $this-&gt;Session-&gt;setFlash('Cannot register a user'); } } } } </code></pre> <p>And in Views, I have created a two files ie <code>index.ctp</code> and <code>register.ctp</code> with a directory <code>Users</code> in View directory</p> <p>code content of <code>register.ctp</code></p> <h2>register.ctp</h2> <pre><code>&lt;?php echo $this-&gt;Form-&gt;create('User', array('type'=&gt;'file')); echo $this-&gt;Form-&gt;input('first_name', array('label'=&gt;'First Name')); echo $this-&gt;Form-&gt;input('last_name', array('label'=&gt;'Last Name')); echo $this-&gt;Form-&gt;input('email'); echo $this-&gt;Form-&gt;input('doc_file', array('type'=&gt;'file')); echo $this-&gt;Form-&gt;end('Register'); ?&gt; </code></pre> <p>And when run this page, and fill up all information, it gives an error</p> <pre><code>Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list' </code></pre> <p>And below is the query I got, where it is inserting an <code>array</code> into <code>doc_file</code></p> <pre><code>SQL Query: INSERT INTO `database_db`.`users` (`first_name`, `last_name`, `email`, `doc_file`) VALUES ('master', 'shifu', 'shifu@k.com', Array) </code></pre> <p>What i am trying :</p> <p>While user is registering, the file name should be random and it should be move to </p> <p><code>localhost/mysite/app/webroot/files/user_data/</code> or <code>localhost/mysite/app/webroot/files/user_data/user_id_directory/</code></p> <p>its good, if it creates the userid directory and storing the file in its parent user directory</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload