<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');



class Multiple_upload 

{

	private $ci ;



    function __construct()

    {

		$this->ci = & get_instance();

        

        // load the libraries at first

         $this->ci->load->helper(array('form', 'url'));

         $this->ci->load->library('upload');

         $this->ci->load->library('image_lib');

    }

    





function multiple_image_upload(array $_files, $tablename, $type, $lastid, $redirectpage)

{

	$finalrpt="";

	$error=0;

	

	$count = count($_files['name']);

	

	for( $i=0 ; $i<$count ; $i++ )

	{

	

		if((($_files['type'][$i]=="image/gif") || ($_files['type'][$i]=="image/jpeg") || ($_files['type'][$i]=="image/pjpeg")) 

				&& ($_files['size'][$i] < 1*1048576))

		{

			if ($_files['error'][$i] > 0)

			{

			$finalrpt.= "Error:".$_files['error'][$i]."<br />";

			}

			else

			{

				

						

			$ext='';

			switch($_files['type'][$i])

			{

				case "image/gif" :	$ext='.gif';break;

				case "image/jpeg" :	$ext='.jpg';break;

				case "image/png" :	$ext='.png';break;

				default : $ext = '.jpg';break;

			}

			

			

				$qry = "select max(gid) from `$tablename` where `aid`='$lastid' ";

				$res = mysql_query($qry) or die(mysql_error());

				$row = mysql_fetch_array($res);

				$maxgid = $row[0];  

				

				$qry = "select `imgname` from `$tablename` where `gid`='$maxgid' ";

				$res = mysql_query($qry) or die(mysql_error());

				$row = mysql_fetch_array($res);

				$maximg = $row['imgname']; 

				

				

				if($maximg!='')

				{

					list($a,$b,$z) = explode('_',$maximg);

					list($c,$d) = explode('.',$z);

					$c++;

					

					$himage = $type.'_'.$lastid.'_'.$c.$ext ;

					

					move_uploaded_file($_files['tmp_name'][$i],"gallery/".$himage);

					$thumb = thumb($himage,$lastid,$c);

				}

				else

				{

					$himage = $type.'_'.$lastid.'_'.$i.$ext ;

					

					move_uploaded_file($_files['tmp_name'][$i],"gallery/".$himage);

					$thumb = thumb($himage,$lastid,$i);

				}



			 			

			if(!image_exist($himage))

			{

			$insert_row = mysql_query("insert into `$tablename`(`aid`,`imgname`,`tmb_img`) values ('$lastid','$himage','$thumb')") or die(mysql_error());

			}

			

			}//else

		}

		else

		{

		

			if($_files['size'][$i]!=0)

			{  

			$finalrpt.="<br>Invalid or no Image&nbsp;, check image size/format.";  							            }

			$error++;



		}

				

	}//end for

	

	if( $error==0 )

	{



	$finalrpt.="<font color='green'>Images uploaded successfully.</font><meta http-equiv='Refresh' content='1; URL=".$redirectpage."'/>";

	}

	

    return $finalrpt;

}



}





/* End of file upload.php */

/* Location: ./application/controllers/upload.php */



