在cake php中的“webroot / img”文件夹中上传图片

"I want to upload images in webroot/img folder in cake php.but i am not able to suggest me some solution as i am new to cake php.I tried a lot of solutions from my side but nothing works.so help me"

my HomesController.php code as

public function admin_add() {

    if ($this->request->is('post')) {
        $this->Home->create();
         $image = $this->request->data['Home']['image'];
         //$imageTypes = array("image/gif", "image/jpeg", "image/png");
         //$uploadFolder = "upload";
         //$uploadPath = WWW_ROOT . $uploadFolder;

         pr($this->request->data);
         //echo $image['type'];
         if ($this->Home->save($this->request->data)) {
            $this->Session->setFlash(__('The home has been saved.'));
            //echo pr($this->request->data);
            //echo die(debug($this->request->data));
            //return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The home could not be saved., tryagain '));
        }
    }
}

admin_add.ctp code is

<div class="homes form">
<?php echo $this->Form->create('Home', array('type' => 'file')); ?>
<fieldset>
    <legend><?php echo __('Add Home content'); ?></legend>
<?php
    echo $this->Form->input('title');
    echo $this->Form->input('body');
    echo $this->Form->input('image');
?>
</fieldset>


<?php echo $this->Form->end(__('Submit')); 

?>
</div>

Blockquote

whilr running code i am not able to upload images and while printing image data I m getting error.

Output:



`Array
    (
    [Home] => Array
        (
            [title] => vhbk
             [body] => ,bjljbl.
             [image] => Array
            (
                [name] => DSC03371.JPG
                [type] => 
                [tmp_name] => 
                [error] => 1
                [size] => 0
            )

         )

     )`

Please refer this : http://www.w3schools.com/php/php_file_upload.asp

Need to check below things:

  1. form attriubute must be set enctype="multipart/form-data"
  2. move_uploaded_file($_FILES["file"]["tmp_name"], "img/" . $_FILES["file"]["name"]);

You can also use $_SERVER['DOCUMENT_ROOT'] to change path in above function.