Page 1 of 1

Cake PHP file upload

Posted: Tue Dec 14, 2010 10:17 am
by devilinc
when i try to upload a file, the end result is the filename is stored in database table but the file is not uploaded to the images/logo folder and hence my file isnt displayed.....where did i go wrong?

my ctp file is:

Code: Select all

<?php echo $form->create('Customer',array('type' => 'file'));?>
        <div class="definetemplate">
          <div class="templatename">
            <h2>1. Enter Customer Name</h2>
            <p>&nbsp;</p>
          </div>
          <div class="formcontainer">
            <div class="clearfix">

 <?php echo $form->input('customer_name', array('label'=>"Customer Name", 'size'=>'60'));?>
            <span id="msgbox" style="display:none"></span></div>
          </div>

           <div class="templatename">
            <h2>2. Monthly Amount Limit </h2>
            <p>&nbsp;</p>
          </div>



		  <div class="formcontainer">
            <div class="clearfix">

             <?php echo $form->input('customer_monthlyamount', array('label'=>"Amount $", 'size'=>'30'));?>
            <span id="msgbox" style="display:none"></span></div>
          </div>

			 

			 <!-- begin input div -->
			 	<div class="templatename">
					<h2>11. Customer Logo </h2>
					<p>&nbsp;</p>
          		</div>

			 	<div class="formcontainer">
            		<div class="clearfix">
					<?php echo $form->input('customer_logo',array('type'=>'file'));?>
				        <span id="msgbox" style="display:none"></span>
					</div>
          		</div>
			 <!-- end input div -->

          <div class="addbtn">
             <!--<input type="image" name="addgroup" value="Add to Database" src="images/addgroupbtn.png" > -->
			 <?php echo $form->button('Add to Database', array('type'=>'image','src'=>'','label' => false));?>

             <!--<a href="#"><img src="images/addtemplatebtn.png" width="116" height="29" align="top" /></a> -->
          </div>
        </div>
      <?php echo $form->end(); ?>
my controller file function is:

Code: Select all

function admin_customerscreate($id = null){
			ob_start();

		  /*App::import('Vendor', 'SimpleImage', array('file' => 'resize'.DS.'resizeclass.php'));
		$image = new SimpleImage();
		$qShowStatus = $this->Customer->query("SHOW TABLE STATUS LIKE 'customers'");
		//	print_r($qShowStatus);
			$id_val =$qShowStatus[0]['TABLES']['Auto_increment'];*/
			  if (!empty($this->data))
			 {
				/*if(is_uploaded_file($this->data['Customer']['customer_logo']['tmp_name']))
				  {
					  $ori_name=$id_val."-".$this->data['Customer']['customer_logo']['name'];
					  $up_preview =WWW_ROOT.'files/customer/preview/'.$ori_name;
    				  move_uploaded_file($this->data['Customer']['customer_logo']['tmp_name'], $up_preview);
						$image->load($up_preview);
						$image->resize(20,20);
						$image->save(WWW_ROOT.'files/customer/thumb/'.$ori_name);
				  }*/
				$this->Customer->create();
				 $currentdate=date("Y-m-d h:i:s");
				 $this->data['Customer']['customer_createdate']=$currentdate;
			    $this->data['Customer']['customer_status']=1;
			  	$this->data['Customer']['department_id'] = $_SESSION['department_id'];


				$ori_name=$this->data['Customer']['customer_logo']['name'];
				$this->data['Customer']['customer_logo'] = $ori_name;
				$target_path =WWW_ROOT.DS.'images/logo/'.$ori_name;
				move_uploaded_file($this->data['Customer']['customer_logo']['tmp_name'], $target_path);
				/*
				$ori_name=$this->data['Customer']['customer_logo']['name'];
				$up_preview =WWW_ROOT.DS.'files/customer/preview/'.$ori_name;
    			move_uploaded_file($this->data['Customer']['customer_logo']['tmp_name'], $up_preview);
    			*/

				//$target_path = "uploads/";
//				$this->webroot . 'images'/logo/
				//$target_path = WWW_ROOT.DS.'img/'.$this->data['Customer']['customer_logo']['name'];
				//echo 'file name is===>'.$this->data['Customer']['customer_logo'];die();
				//echo $target_path = $this->webroot.'app/webroot/images/logo/'.$this->data['Customer']['customer_logo']['name'];
				 //$up_preview =WWW_ROOT.'/files/realteam/preview/'.$ori_name;
				if ($this->Customer->save($this->data)) {

					  //$data = $this->paginate('Customer');
					//$this->redirect(array('action' => '/customerslist/'.$_SESSION['department_id']));

					  $this->Session->setFlash(__('The Customer has been saved', true));
					   $data = $this->paginate('Customer');
					 	 $this->redirect(array('action' => '/customerslist/'.$data[0]['Customer']['department_id']));
					  unset($_SESSION['department_id']);
					  ob_flush();
				   } else
				   {
					  $this->Session->setFlash(__('The Customer could not be saved. Please, try again.', true));
					  $data = $this->paginate('Customer');
			$this->redirect(array('action' => '/customerscreate/'.$data[0]['Customer']['department_id']));

			}}else{
					$_SESSION['department_id']=$id;
				}

	}