Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • To upload images in CakePHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 164
    Comment on it

    We can add the functionality of browsing images and let the user upload images of its choice to his account.

    For this functionality we first have to give a browse button in the registration page and then the functionality to it of adding the images to a specific folder on the server.

    <input name="files[]" type="file"  id="maximgupload" class="carimagemulti multi with-preview max-<?php echo $maximg; ?>

    This we use in the ctp file to add images. It has the type file which gives the browse option to the user.

    To upload the file we first add function to the controller where we give path of the folder in which we are going to store the images.

    public function uploadImages($fileArray, $validityArray, $userId, $imageType = '')
    	{ 
    	 $fileCount = sizeof($fileArray['name']);
    
    	 // $imgpath = ($imageType == 'licence') ? IMAGE_URL . "licence/" : IMAGE_URL . "carimages/";
    
    	
        
        	$imgpath = IMAGE_URL . "carimages/";
        
    
        // echo $imgpath; die;
    
    
    		
    			 $file_name = $fileArray['name'][$i];
    
                if ($file_name) {
                   //$file_tmp_name = $this->request->params['form']['store_image']['tmp_name'];
    
                    $priv1 = 0777;
                    
                    if (!file_exists($imgpath)) {
                        mkdir($imgpath, $priv1) ? true : false; // creates a new directory with write permission. 
                    }
    
                    $pathInfo = pathinfo($file_name);
                    $fname = $pathInfo['filename'];
                    $ext = $pathInfo['extension'];
                    $ImgName = @strtolower($fname);
                    $newImgName = $ImgName . "_" . time() . rand(); //adding time stamp for unique name we get in the database.
    
                    $file["name"] = $fileArray['name'][$i];
                    $file['type'] = $fileArray['type'][$i];
                    $file['tmp_name'] = $fileArray['tmp_name'][$i];
                    $file['error'] = $fileArray['error'][$i];
                    $file['size'] = $fileArray['size'][$i];
    
    // type of image type we want to add 
                    if ($ext == 'gif') {
                        $result = $this->Upload->upload($file, $imgpath, $newImgName . ".gif", array('type' => 'resizemin', 'size' => array('940', '300')));
                        $name = $newImgName . ".gif";
                    } else if ($ext == 'jpeg') {
                        $result = $this->Upload->upload($file, $imgpath, $newImgName . ".jpeg", array('type' => 'resizemin', 'size' => array('940', '300')));
                        $name = $newImgName . ".jpeg";
                    } else if ($ext == 'png') {
                        $result = $this->Upload->upload($file, $imgpath, $newImgName . ".png", array('type' => 'resizemin', 'size' => array('940', '300')));
                        $name = $newImgName . ".png";
                    } else {
                        $result = $this->Upload->upload($file, $imgpath, $newImgName . ".jpg", array('type' => 'resizemin', 'size' => array('940', '300')));
                        $name = $newImgName . ".jpg";
                    }
    
                    
                    
                    else
                    
                    	// Car Images
                    	$temp = array();
                    	$temp["User"]['user_id'] = $userId;
                    	$temp["User"]['image_name'] = $name;// final name of the file will be stored
                    	$this->User->create();
    					$this->User->save($temp);
    
                    
                }
    		
    
    	}

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: