title: Cake PHP image upload
body: <p>I'm new to cake PHP and I need some help with uploading an image. I need to allow a user to upload an image and I want that image to be saved in the directory(www/CakePHP/app/webroot/img/), and also would like the database to store the file path of the image. This is what I have got so far,</p>
  
  <p>ProductsADD.ctp:</p>
  
  <pre><code>&lt;div class="products form"&gt;
  &lt;?php echo $this-&gt;Form-&gt;create('Product'); ?&gt;
  
  
      &lt;fieldset&gt;
          &lt;legend&gt;&lt;?php echo __('Add Product Details'); ?&gt;&lt;/legend&gt;
      &lt;?php
          echo $this-&gt;Form-&gt;input('product_name', array('required'=&gt;false));
          echo $this-&gt;Form-&gt;input('product_model_number', array('required'=&gt;false));
          echo $this-&gt;Form-&gt;input('product_brand', array('required'=&gt;false));
          echo $this-&gt;Form-&gt;input('product_description', array('required'=&gt;false));
          echo $this-&gt;Form-&gt;input('price_bronze', array('required'=&gt;false));
          echo $this-&gt;Form-&gt;input('price_silver', array('required'=&gt;false));
          echo $this-&gt;Form-&gt;input('price_gold', array('required'=&gt;false));
          echo $this-&gt;Form-&gt;input('upload', array('type'=&gt;'file'));
  
  
      ?&gt;
      &lt;/fieldset&gt;
  &lt;?php echo $this-&gt;Form-&gt;end(__('Submit')); ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>productscontroller:</p>
  
  <pre><code>   public function add() {
              if ($this-&gt;request-&gt;is('post')) {
                  $this-&gt;Product-&gt;create();
                  if ($this-&gt;Product-&gt;save($this-&gt;request-&gt;data)) {
                      $this-&gt;Session-&gt;setFlash(__('The product has been saved.'));
                      return $this-&gt;redirect(array('action' =&gt; 'index'));
                  } else {
                      $this-&gt;Session-&gt;setFlash(__('The product could not be saved. Please, try again.'));
                  }
                  if(!empty($this-&gt;data))
                  {
                      //Check if image has been uploaded
                      if(!empty($this-&gt;data['products']['upload']['name']))
                      {
                          $file = $this-&gt;data['products']['upload']; //put the data into a var for easy use
  
                          $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
                          $arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions
  
                          //only process if the extension is valid
                          if(in_array($ext, $arr_ext))
                          {
                              //do the actual uploading of the file. First arg is the tmp name, second arg is
                              //where we are putting it
                              move_uploaded_file($file['tmp_name'], WWW_ROOT . 'CakePHP/app/webroot/img/' . $file['name']);
  
                              //prepare the filename for database entry
                              $this-&gt;data['products']['product_image'] = $file['name'];
                          }
                      }
  
                      //now do the save
                      $this-&gt;products-&gt;save($this-&gt;data) ;
                  }
              }
  
          }
  </code></pre>
  
  <p>I can't seem to figure out why the uploaded image does not get saved to the directory. Can someone please help.</p>