Multi file upload in cakephp

<p>Multiupload file in cakephp,file resize in cakephp,imagecrop in cakephp,multiple file upload in cakephp,multiple resize image in cakephp</p>
1. first create view file and create form category.ctp


<?php echo $this->Form->create('Category',array('type'=>'file','novalidate'=>true,'inputDefaults'=>array('div'=>false,'label'=>false)));?>

<?php echo  $this->Form->input('category_name',array('class'=>'inp-form'));?>
<?php echo  $this->Form->input('category_image',array('type'=>'file','class'=>''));?>
<?php echo  $this->Form->submit('Submit',array('class'=>'form-submit'));?>
<?php echo  $this->Form->end();?>
</div>
2. create a config file in app->config->config.php and set the path of images
<div class="code">
<?php
$config['ADMIN_PAGINATION'] = 8;
$config['CATEGORY.MAIN_IMAGE'] = WWW_ROOT.'img/category/';
$config['CATEGORY.THUMB'] = WWW_ROOT.'img/category/thumb/';
$config['CATEGORY.LARGE'] = WWW_ROOT.'img/category/large/';
$config['CATEGORY.THUMB_WIDTH'] = 50;
$config['CATEGORY.THUMB_HEIGHT'] = 50;
$config['CATEGORY.LARGE_WIDTH'] = 100;
$config['CATEGORY.LARGE_HEIGHT'] = 75;

3.create a common components in app->Controller->Component->uploadcomponent.php it is libary file

<?php
class UploadComponent extends Component{
public $_file;
public $_extension;
public $_destination;
public $_name;
public function upload($file=null,$destination=null,$rules=null){

$this->result = false;
$this->_file = $file;
$this->_destination = $destination;
if (substr($this->_destination, -1) != '/') {
$this->_destination .= '/';
}

$fileName = $this->uniquename($destination . $file['name']);
$fileTmp = $file['tmp_name'];
$fileSize = $file['size'];
$fileType = $file['type'];
$fileError = $file['error'];
$this->_name = $fileName;
$this->_extension = strtolower($this->getExtension($this->_name));
/* pr($this->_name);
echo $this->_extension;
pr($fileTmp);die; */

if (is_uploaded_file($fileTmp)) {
if ($rules == NULL) {
$output = $fileName;
// -- just upload it
if (move_uploaded_file($fileTmp, $output)) {
chmod($output, 0644);

$this->result = basename($this->_name);
}
}else{
//echo "hi";
// -- gd lib check
if (function_exists("imagecreatefromjpeg")) {

// -- handle it based on rules
if (isset($rules['type']) && isset($rules['size'])) {
$this->resize($this->_file,$rules['size'], $this->_extension);
} else {
$this->error("Invalid \"rules\" parameter");
}
} else {
$this->error("GD library is not installed");
}
}
}

}
public function resize($file=null,$size=null,$type,$output = NULL){
if (is_null($output))
$output = 'jpg';

if($type=="jpg" || $type=="jpeg" )
{
$src = imagecreatefromjpeg($file['tmp_name']);

}
else if($type=="png")
{
$src = imagecreatefrompng($file['tmp_name']);

}
else
{
$src = imagecreatefromgif($file['tmp_name']);
}

list($width,$height)=getimagesize($file['tmp_name']);

if (is_array($size)) {
$maxW = intval($size[0]);
$maxH = intval($size[1]);
}
$dstImg = imagecreatetruecolor($maxW, $maxH);
$white = imagecolorallocate($dstImg, 255, 255, 255);
imagefill($dstImg, 0, 0, $white);
imagecolortransparent($dstImg,$white);

imagecopyresampled($dstImg,$src,0,0,0,0,$maxW,$maxH,$width,$height);
// -- try to write
switch ($output) {
case 'jpg':
case 'JPG':
$write = imagejpeg($dstImg, $this->_name);
break;
case 'png':
case 'PNG':
$write = imagepng($dstImg, $this->_name . ".png");
break;
case 'gif':
case 'GIF':
$write = imagegif($dstImg, $this->_name . ".gif");
break;
}

// -- clean up
imagedestroy($dstImg);

if ($write) {
$this->result = basename($this->_name);
} else {
echo 'error in file uploading';
}


}
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
public function uniquename($file) {
$parts = pathinfo($file);
$dir = $parts['dirname'];
$file = ereg_replace('[^[:alnum:]_.-]', '', $parts['basename']);
$ext = $parts['extension'];
if ($ext) {
$ext = '.' . $ext;
$file = substr($file, 0, -strlen($ext));
}
$i = 0;
while (file_exists($dir . '/' . $file . $i . $ext))
$i++;
return $dir . '/' . $file . $i . $ext;
}
}

?>



4. controller code


class CategoriesController extends AppController {

var $components = array('Upload');

public function beforeFilter() {
parent::beforeFilter();
//$this->Auth->allow(array('admin_add'));
}


public function admin_add() {
//echo $this->Upload->add(15,6);
if ($this->request->is('post')) {
//pr($this->request->data);die;
$this->Category->set($this->request->data);

if ($this->Category->validates()) {
$file = array();
if (isset($this->request->data['Category']['category_image']['name']) && $this->request->data['Category']['category_image']['error'] == 0) {
$file = $this->request->data['Category']['category_image'];
$thumb = array('size' => array(Configure::read('CATEGORY.THUMB_WIDTH'), Configure::read('CATEGORY.THUMB_HEIGHT')), 'type' => 'resize');
//pr($thumb); die;
$large = array('size' => array(Configure::read('CATEGORY.LARGE_WIDTH'), Configure::read('CATEGORY.LARGE_HEIGHT')), 'type' => 'resize');
$res = $this->Upload->upload($file, Configure::read('CATEGORY.THUMB') . DS, $thumb);
$large = $this->Upload->upload($file, Configure::read('CATEGORY.LARGE') . DS, $large);
$res1 = $this->Upload->upload($file, Configure::read('CATEGORY.MAIN_IMAGE') . DS);
if ($this->Upload->result) {
//echo $this->Upload->result;die;
$this->request->data['Category']['category_image'] = $this->Upload->result;
}
}
//pr($this->request->data);die;
if ($this->Category->save($this->request->data)) {
$this->Session->setFlash('Category created successfully');
$this->redirect(array('action' => 'cat_index'));
} else {
$this->Session->setFlash('Please try again! correct errors.');
}
} else {
//echo 'hi';
}
}
}

}

5. now create a folder in app->webroot->img->category->large and app->webroot->img->category->thumb

Share Button

About admin

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Author: pramod kumar kumawat Mob: 9269727062
Free WordPress Themes - Download High-quality Templates