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
    • 204
    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.

    1. <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.

    1. public function uploadImages($fileArray, $validityArray, $userId, $imageType = '')
    2.     {
    3.      $fileCount = sizeof($fileArray['name']);
    4.  
    5.      // $imgpath = ($imageType == 'licence') ? IMAGE_URL . "licence/" : IMAGE_URL . "carimages/";
    6.  
    7.     
    8.     $imgpath = IMAGE_URL . "carimages/";
    9.  
    10. // echo $imgpath; die;
    11.  
    12.  
    13.         
    14.              $file_name = $fileArray['name'][$i];
    15.  
    16. if ($file_name) {
    17. //$file_tmp_name = $this->request->params['form']['store_image']['tmp_name'];
    18.  
    19. $priv1 = 0777;
    20. if (!file_exists($imgpath)) {
    21. mkdir($imgpath, $priv1) ? true : false; // creates a new directory with write permission.
    22. }
    23.  
    24. $pathInfo = pathinfo($file_name);
    25. $fname = $pathInfo['filename'];
    26. $ext = $pathInfo['extension'];
    27. $ImgName = @strtolower($fname);
    28. $newImgName = $ImgName . "_" . time() . rand(); //adding time stamp for unique name we get in the database.
    29.  
    30. $file["name"] = $fileArray['name'][$i];
    31. $file['type'] = $fileArray['type'][$i];
    32. $file['tmp_name'] = $fileArray['tmp_name'][$i];
    33. $file['error'] = $fileArray['error'][$i];
    34. $file['size'] = $fileArray['size'][$i];
    35.  
    36. // type of image type we want to add
    37. if ($ext == 'gif') {
    38. $result = $this->Upload->upload($file, $imgpath, $newImgName . ".gif", array('type' => 'resizemin', 'size' => array('940', '300')));
    39. $name = $newImgName . ".gif";
    40. } else if ($ext == 'jpeg') {
    41. $result = $this->Upload->upload($file, $imgpath, $newImgName . ".jpeg", array('type' => 'resizemin', 'size' => array('940', '300')));
    42. $name = $newImgName . ".jpeg";
    43. } else if ($ext == 'png') {
    44. $result = $this->Upload->upload($file, $imgpath, $newImgName . ".png", array('type' => 'resizemin', 'size' => array('940', '300')));
    45. $name = $newImgName . ".png";
    46. } else {
    47. $result = $this->Upload->upload($file, $imgpath, $newImgName . ".jpg", array('type' => 'resizemin', 'size' => array('940', '300')));
    48. $name = $newImgName . ".jpg";
    49. }
    50.  
    51. else
    52.     // Car Images
    53.     $temp = array();
    54.     $temp["User"]['user_id'] = $userId;
    55.     $temp["User"]['image_name'] = $name;// final name of the file will be stored
    56.     $this->User->create();
    57.                     $this->User->save($temp);
    58.  
    59. }
    60.         
    61.  
    62.     }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: