Jump to content
xisto Community
Sign in to follow this  
regi

Cakephp - Upload 5 Images

Recommended Posts

Hello,

I have got a question and sorry for my bad englisch at first!
I want to make an Upload with CakePHP, which has to work with the MySQL. But I made something wrong, that just one image can be uploaded and I don't find the mistake. In google and in the forum I don't find anything that could help me. As well this is a new topic for me, that I hope anybody can help me...


In the MySQL I made this fiels in the table ads:

-img_type, img1_type, img2_type, img3_type, img4_type and data_type


My ads_contoller.php looks like this:

<?phpclass AdsController extends AppController {var $name = 'Ads';var $components = array("obAuth");var $uses = array('Category', 'User', 'Subcategory', 'Ad', 'Setting', 'Invoice', 'Banner');</P><P> function index(){  $this->set('categories', $this->Category->findall() );  $category = $this->Category->findbyid($_GET['id']);  $this->set('categoryname', $category['Category']['name_german']);  $subcategory = $this->Subcategory->findallbycat_id($_GET['id']);  $this->set('subcategories', $subcategory);}</P><P>/** Adding ads* Create: 12.07.2007* TODO:*/function add(){  $this->obAuth->lock(array(1,2)); //User logged in?</P><P>  $settings_commercial = $this->Setting->findbyname('commercial');  $this->set('settings_commercial', $settings_commercial['Setting']['value']);  $settings_commercial_value = $settings_commercial['Setting']['value'];</P><P>  $settings_Price_yellowbg = $this->Setting->findbyname('Price_yellowbg');  $this->set('settings_Price_yellowbg', $settings_Price_yellowbg['Setting']['value']);  $settings_Price_yellowbg_value = $settings_Price_yellowbg['Setting']['value'];</P><P>  $settings_Price_top = $this->Setting->findbyname('Price_top');  $this->set('settings_Price_top', $settings_Price_top['Setting']['value']);  $settings_Price_top_value = $settings_Price_top['Setting']['value'];</P><P>  $settings_Price_ad = $this->Setting->findbyname('Price_ad');  $this->set('settings_Price_ad', $settings_Price_ad['Setting']['value']);  $settings_Price_ad_value = $settings_Price_ad['Setting']['value'];</P><P>  // Validate Form  if(isset($this->data['Ad']))     {</P><P>   if (empty($this->data['Ad']['subcat_id']) or empty($this->data['Ad']['title']))   {    $this->set("error","1");   }   else   {    //MySQL inserts    if($_FILES['file']['tmp_name'])    { 	switch ( $_FILES['file']['type'] )      {   	case "image/gif":        $this->data['Ad']['img_type'] = "gif";   	break;      case "image/jpeg":        $this->data['Ad']['img_type'] = "jpeg";   	break;   	case "image/png":        $this->data['Ad']['img_type'] = "png";   	break;   	case "image/jpg":        $this->data['Ad']['img_type'] = "jpg";   	break;   	default:        $this->data['Ad']['img_type'] = "";   	break;      }//end switch    }//end if        if($_FILES['file3']['tmp_name'])    { 	switch ( $_FILES['file3']['type'] )      {      case "image/gif":        $this->data['Ad']['img1_type'] = "gif";   	break;      case "image/jpeg":        $this->data['Ad']['img1_type'] = "jpeg";   	break;   	case "image/png":        $this->data['Ad']['img1_type'] = "png";   	break;   	case "image/jpg":        $this->data['Ad']['img1_type'] = "jpg";   	break;      }//end switch    }//end if        if($_FILES['file4']['tmp_name'])    { 	switch ( $_FILES['file4']['type'] )      {   	case "image/gif":        $this->data['Ad']['img2_type'] = "gif";   	break;      case "image/jpeg":        $this->data['Ad']['img2_type'] = "jpeg";   	break;   	case "image/png":        $this->data['Ad']['img2_type'] = "png";   	break;   	case "image/jpg":        $this->data['Ad']['img2_type'] = "jpg";   	break;      }//end switch    }//end if</P><P>    if($_FILES['file2']['tmp_name'])    { 	switch ( $_FILES['file2']['type'] )      {   	case "application/msword":   	$this->data['Ad']['data_type'] = "doc";   	break;      case "application/pdf":   	$this->data['Ad']['data_type'] = "pdf";   	break;   	default:        $this->data['Ad']['data_type'] = "";   	break;      }//end switch    }//end if</P><P>    $this->data['Ad']['startdate'] = date("d M Y", time());    $this->data['Ad']['content'] = strip_tags($this->data['Ad']['content']);//Kein HTML    $users_id = $_SESSION['mYpERsOnALhaSHkeY']['User']['id'];    $this->data['Ad']['user_id'] = $users_id;    $this->Ad->save($this->data['Ad']);    $last_id = $this->Ad->getLastInsertId();</P><P>    //and file upload    $root = WWW_ROOT;</P><P>    if($_FILES['file']['tmp_name']) 	{      switch ( $_FILES['file']['type'] )      {   	case "image/gif":      move_uploaded_file($_FILES['file']['tmp_name'], $root."upload/".$last_id.".gif");        chmod ($root."upload/".$last_id.".gif", 0755);   	break;      case "image/jpeg":        $this->gd_resize($_FILES['file']['tmp_name'], "jpeg", $last_id);        move_uploaded_file($_FILES['file']['tmp_name'], $root."upload/".$last_id."_full.jpeg");        chmod ($root."upload/".$last_id.".jpeg", 0755);        chmod ($root."upload/".$last_id."_full.jpeg", 0755);   	break;   	case "image/png":        move_uploaded_file($_FILES['file']['tmp_name'], $root."upload/".$last_id."_full.png");        copy($root."upload/".$last_id."_full.png",  $root."upload/".$last_id.".png");        chmod ($root."upload/".$last_id."_full.png", 0755);        chmod ($root."upload/".$last_id.".png", 0755);   	break;   	default:        $this->set("error","2"); //File not allowed   	break;      } 	}</P><P>    if(isset($_FILES['file2']['tmp_name'])) 	{      switch ( $_FILES['file2']['type'] )      {   	case "application/msword":        move_uploaded_file($_FILES['file2']['tmp_name'], $root."upload/".$last_id.".doc");        chmod ($root."upload/".$last_id.".doc", 0755);   	break;      case "application/pdf":        move_uploaded_file($_FILES['file2']['tmp_name'], $root."upload/".$last_id.".pdf");        chmod ($root."upload/".$last_id.".pdf", 0755);   	break;   	default:        $this->set("error","2"); //File not allowed   	break;      } 	}        if(isset($_FILES['file3']['tmp_name'])) 	{      switch ( $_FILES['file3']['type'] )      {   	case "image/jpg":        move_uploaded_file($_FILES['file3']['tmp_name'], $root."upload/".$last_id.".jpg");        chmod ($root."upload/".$last_id.".jpg", 0755);   	break;      case "image/gif":        move_uploaded_file($_FILES['file3']['tmp_name'], $root."upload/".$last_id.".gif");        chmod ($root."upload/".$last_id.".gif", 0755);   	break;   	default:        $this->set("error","2"); //File not allowed   	break;      } 	}        if(isset($_FILES['file4']['tmp_name'])) 	{      switch ( $_FILES['file4']['type'] )      {   	case "image/jpg":        move_uploaded_file($_FILES['file3']['tmp_name'], $root."upload/".$last_id.".jpg");        chmod ($root."upload/".$last_id.".jpg", 0755);   	break;      case "image/gif":        move_uploaded_file($_FILES['file3']['tmp_name'], $root."upload/".$last_id.".gif");        chmod ($root."upload/".$last_id.".gif", 0755);   	break;   	default:        $this->set("error","2"); //File not allowed   	break;      } 	}</P><P> 	//Not defined checkboxes check    if (!isset($this->data['Ad']['topcat']))    { 	$this->data['Ad']['topcat'] = "n";    }    if (!isset($this->data['Ad']['bgyellow']))    { 	$this->data['Ad']['bgyellow'] = "n";    }</P><P> 	/*  	* Invoice  	*/ 	if($settings_commercial_value == "1") 	{ 	$total_price = $settings_Price_ad_value; 	if($this->data['Ad']['topcat'] == "j") 	{      $total_price = $settings_Price_top_value + $total_price; 	}</P><P> 	if($this->data['Ad']['bgyellow'] == "j") 	{      $total_price = $settings_Price_yellowbg_value + $total_price; 	} 	$username = $_SESSION['mYpERsOnALhaSHkeY']['User']['username']; 	$sql_invoices = array('Invoice' => array('user_id' => $users_id, 'price' => $total_price, 'ad_id' => $last_id, 'username' => $username)); 	$this->Invoice->save($sql_invoices);</P><P> 	$from_mail = $this->Setting->findbyname('Mailsender'); 	$from_mail = $from_mail['Setting']['value']; 	// Send E-Mail ------------------------------------------ 	//Headers 	$headers = ""; 	$headers .= "From: <".$from_mail.">\n"; 	$headers .= "Reply-To: <".$from_mail.">\n"; 	$headers .= "Return-Path: <".$from_mail.">\n";	// these two to set reply address 	$headers .= "Message-ID: <<A href="mailto:TheSystem@".$_SERVER['SERVER_NAME'].">\n">TheSystem@".$_SERVER['SERVER_NAME'].">\n</A>"; 	$headers .= "X-Mailer: PHP v".phpversion()."\n";</P><P> 	$this->set("price",$total_price); 	ob_start(); 	$this->render(null, 'email_invoice', null); //render /app/views/layouts/smarty/email_invoice.tpl 	$msg = ob_get_clean();</P><P> 	//Sending Mail</P><P> 	mail($username, "Rechnung", $msg, $headers);</P><P> 	}    $this->redirect('/users'); //At the End show the User another Page   }     }</P><P>  $this->set('categories', $this->Category->findall());  $subcategory = $this->Subcategory->findall();  $this->set('subcategories', $subcategory);  $newsubcategory = array();  $testarray = array();  $counter = 0;  foreach ($subcategory as $out)  {   $cat_id = $out['Subcategory']['cat_id'];   $out_id = $out['Subcategory']['id'];   $out_german = $out['Subcategory']['name_german'];   $newsubcategory[$cat_id][$counter]['id'] = $out_id;   $newsubcategory[$cat_id][$counter]['name_german'] = $out_german;   $counter++;  }  $this->set('newsubcategories', $newsubcategory);  $settings_pdf_upload = $this->Setting->findbyname('PDF_Upload');  $this->set('settings_upload', $settings_pdf_upload['Setting']['value']);}</P><P> function admin_delete($id){  $this->obAuth->lock(array(2)); //Only Admin  $this->Ad->del($id);  $this->redirect('/'); //At the End show the User another Page}</P><P>function view($id){  $this->set('categories', $this->Category->findall());  $ad_content = $this->Ad->findbyid($id);  $this->set('ads_first', $ad_content);  $users_id = $ad_content['Ad']['user_id'];  $this->set('users_info', $this->User->findbyid($users_id));            	//Banner-Code start------------------------------------------------            	$banners = $this->Banner->findAllByactive('1');            	$rows = $this->Banner->getAffectedRows();            	if ($rows >= 1)            	{                    	$random_number =rand(0,$rows);                    	if ($random_number != 0)                    	{                            	$random_number = $random_number-1 ;                    	}                    	$this->set('banners_header', $banners[$random_number]);                    	$sql = array();                    	$sql['Banner']['id'] = $banners[$random_number]['Banner']['id'];                    	$sql['Banner']['shows'] = $banners[$random_number]['Banner']['shows'] + 1;                    	if ($banners[$random_number]['Banner']['max_shows'] == $banners[$random_number]['Banner']['shows'])                    	{                            	$sql['Banner']['active'] = "0";                    	}                    	$this->Banner->save($sql['Banner']);            	}            	else            	{                    	$this->set('banners_header', '');            	}            	//Banner-Code end--------------------------------------------------</P><P>}</P><P> function contact($id){  $this->set('categories', $this->Category->findall());  $ad_content = $this->Ad->findbyid($id);  $this->set('ads_first', $ad_content);  $users_id = $ad_content['Ad']['user_id'];  $this->set('users_info', $this->User->findbyid($users_id));</P><P>  if(isset($this->data['contact']))  {   if (empty($this->data['contact']['name']) or empty($this->data['contact']['email']) or empty($this->data['contact']['text'])    	or $_SESSION['captcha_spam'] != $this->data['contact']['capture'])   {    $this->set("error","1");   }   else   { 	//$from_mail = $this->Setting->findbyname('Mailsender'); 	//$from_mail = $from_mail['Setting']['value']; 	$from_mail = $this->data['contact']['email']; 	// Send E-Mail ------------------------------------------ 	//Headers 	$headers = ""; 	$headers .= "From: <".$from_mail.">\n"; 	$headers .= "Reply-To: <".$from_mail.">\n"; 	$headers .= "Return-Path: <".$from_mail.">\n";	// these two to set reply address 	$headers .= "Message-ID: <<A href="mailto:TheSystem@".$_SERVER['SERVER_NAME'].">\n">TheSystem@".$_SERVER['SERVER_NAME'].">\n</A>"; 	$headers .= "X-Mailer: PHP v".phpversion()."\n";</P><P> 	$contact_name = $this->data['contact']['name']; 	$contact_email = $this->data['contact']['email']; 	$contact_text = $this->data['contact']['text']; 	$this->set("contact_name",$contact_name); 	$this->set("contact_email",$contact_email); 	$this->set("contact_text",$contact_text); 	ob_start(); 	$this->render(null, 'email_contact', null); //render /app/views/layouts/smarty/email_invoice.tpl 	$msg = ob_get_clean();</P><P> 	$username = $this->User->findbyid($users_id); 	$username = $username['User']['username']; 	//Sending Mail 	$betreff = $this->data['contact']['betreff']; 	mail($username, $betreff, $msg, $headers); 	$this->redirect('/ads/view/'.$id); //At the End show the User another Page   }  }  //Captcha Code            	$root = WWW_ROOT;            	$dest_file = $root."upload/captcha_bild.png";    $text = $this->randomString(4);  //Die Zahl bestimmt die Anzahl stellen  $_SESSION['captcha_spam'] = $text;  $img = ImageCreateFromPNG($root.'/captcha/captcha.png'); //Backgroundimage  $color = ImageColorAllocate($img, 0, 0, 0); //Farbe  $ttf = $root."/captcha/XFILES.TTF"; //Schriftart  $ttfsize = 25; //Schriftgrösse  $angle = rand(0,5);  $t_x = rand(5,30);  $t_y = 35;  imagettftext($img, $ttfsize, $angle, $t_x, $t_y, $color, $ttf, $text);  imagepng($img, $dest_file);  imagedestroy($img); }</P><P> function gd_resize($file_source, $type, $last_id){  $root = WWW_ROOT;  $max_size = 200;  $dest_file = $root."upload/".$last_id.".".$type;   	// set output file  $image_file = $file_source;   	// set source file  $size = getimagesize($image_file);   	// get original size  //print_r ($size);  if($size[0] > $size[1]) {	$divisor = $size[0] / $max_size;  }  else {	$divisor = $size[1] / $max_size;  }   	// to get allways pictures of the same size, which ist   	// mostly wanted in imageviewers, look what ist larger:   	// width or height</P><P>  $new_width = $size[0] / $divisor;  $new_height = $size[1] / $divisor;   	// set new sizes</P><P>  settype($new_width, 'integer');  settype($new_height, 'integer');   	// sizes should be integers  switch ( $type )  {	case 'jpeg':   $image_big = imagecreatefromjpeg($image_file);   break;	case 'gif':   $image_big = imagecreatefromgif($image_file);     break;  }  // New Image with white background and box  $image_small = imagecreatetruecolor(110, 80);  $farbe_body = imagecolorallocate ( $image_small, 255, 255, 255 );  imagefill ( $image_small, 0, 0, $farbe_body );</P><P>  $farbe_b = imagecolorallocate ( $image_small, 186, 186, 186 );  imageline ( $image_small, 0, 0, 110, 0, $farbe_b );  imageline ( $image_small, 0, 0, 0, 80, $farbe_b );  imageline ( $image_small, 109, 0, 109, 80, $farbe_b );  imageline ( $image_small, 0, 79, 110, 79, $farbe_b );    // Resize Image  if ($new_width >= $new_height)  {   imagecopyresampled($image_small, $image_big, 0,0, 0,0, 110,80, $size[0],$size[1]);  }  else  {   imagecopyresampled($image_small, $image_big, 25,0, 0,0, 60, 80, $size[0],$size[1]);  }  imagedestroy($image_big);   	// the original data are no longer used</P><P>  switch ( $type )  {	case 'jpeg':   imagejpeg($image_small, $dest_file, 100);	break;	case 'gif':  	imagejpeg($image_small, $dest_file);	break;  }}</P><P> // Captcha Function RandomStringfunction randomString($len) {  srand($this->make_seed());                 		  //Der String $possible enthält alle Zeichen, die verwendet werden sollen  $possible="ABCDEFGHJKLMNPRSTUVWXYZ23456789";  $str="";  while(strlen($str)<$len)   {   $str.=substr($possible,(rand()%(strlen($possible))),1);  }  return($str);}</P><P> function make_seed(){  list($usec , $sec) = explode (' ', microtime());  return (float) $sec + ((float) $usec * 100000);}</P><P>}?>

and this is my add.tpl with the <input> code:

<tr><td valign="top">Foto hinzufügen</td><td>  <input type="hidden" value="500000" name="max_file_size" />  <input type="file" style="width: 250px;" name="file" /><br/>  <input type="hidden" value="500000" name="max_file_size" />  <input type="file" style="width: 250px;" name="file3" /><br/>  <input type="hidden" value="500000" name="max_file_size" />  <input type="file" style="width: 250px;" name="file4" /><br/>  <font color="#888888">nur GIF-, JPG- und PNG-Bilder mit max. 200 KB</font></td></tr></P><P>{if $settings_upload == "1"}<tr><td valign="top">Weitere Anlage</td><td>  <input type="hidden" value="500000" name="max_file_size" />  <input type="file" style="width: 250px;" name="file2" /><br/>  <font color="#888888">nur PDF- und DOC-Dokumente mit max. 200 KB</font></td></tr>{/if}

I really hope, that somebody knows whats wrong here or if I have forget something.

Best regards
regi
Edited by regi (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.