2014-04-29 6 views
0

Ceci est mon contrôleurComment télécharger plusieurs images à la fois avec un seul bouton dans Zend Framework 1.12?

contrôleur d'image:

class Admin_xxxxController extends Zend_Controller_Action 
{ 

    public function init() 
    { 
     $this->_helper->layout()->setLayout('admin'); 
     if ($this->_helper->FlashMessenger->hasMessages()) { 
      $messages = $this->_helper->FlashMessenger->getMessages(); 
      $this->view->messages = $messages[0]; 
     } 
    } 

    public function preDispatch() 
    { 
     if(!Zend_Auth::getInstance()->hasIdentity()) { 
      if ('login' != $this->getRequest()->getActionName()) { 
       $this->_helper->redirector('index','login'); 
      } else { 

     } 
    } 
    parent::preDispatch(); 
} 

    public function saveAction() 
    { 
     try { 
      if(isset($_POST['submit'])) { 
       $i=0; 
       $lmgdata=array(); 
       foreach($_FILES as $k=>$v){ 
        $post=$this->_request->getPost(); 
        $path = "images/landimages"; 
        $thumb="images/landimages/thumbnails"; 
        if(!is_dir($path)){ // to check is folder exist or not 
         mkdir($path,'0777',true); 
        } 

        if(!is_dir($thumb)){ // to check is folder exist or not 
         mkdir($thumb,'0777',true); 
        } 

      $maxWidth="600"; $maxHeidht="500";$thumbwidth="100";$thumbheidht="75"; 
      list($width,$height,$y) = getimagesize($v['tmp_name'][$i]); 
      $scale = min($maxWidth/$width, $maxHeidht/$height); 

      $scale1 = min($thumbwidth/$width, $thumbheidht/$height); 

      $s1=$v['name'][$i]; $destpath = $path.'/'.$s1; $thumbpath = $thumb.'/'.$s1; 

      if(move_uploaded_file($v['tmp_name'][$i],$destpath)){ 
        //$this->resize($thumbpath, $destpath,$width, $height,$scale1,1); 
        $this->resize($destpath, $destpath,$width, $height,$scale,0); 
        $lmgdata[$i]['lm_image']=$s1; // to save new image name in db 
       }else{ 
        echo "not uploaded"; die(); 
       } 
       $i++; 
       } 
      } 

       $post=$this->_request->getPost(); 
       $data=array(); 
       if($post['land_id']) 
       $data['land_id']=$post['land_id'];     
       $mapper= new Application_Model_xxxMapper(); 
       $lmmapper= new Application_Model_yyyMapper(); 

        if($lid = $mapper->save($data)){ 
         $lmmapper->save($lmgdata, $lid); 
         $this->_helper->FlashMessenger(array('success'=>'xxx added Sucessfully')); 
        }else{ 
         $this->_helper->FlashMessenger(array('failed'=>'xxx not added ,please try again')); 
        }      

       $this->_helper->redirector('index','xxxx','admin');      
     } 
     catch(Zend_Db_Exception $e) 
      { 

     echo $e->getMessage(); 

      } 
} 

Ceci est mon formulaire d'ajout où je télécharger plusieurs images Ajouter forme:

<form action="<?php echo $this->url(array('controller'=>'lands','action'=>'save'),'admin',true);?>" method="post" enctype="multipart/form-data"> 
    Upload Images:<input type="file" multiple name="land_image[]" id="land_image" class="form-control" autocomplete="off" title=""> 

I utilisé pour la boucle i n contrôleur pour enregistrer plusieurs images et Lorsque j'ai essayé de télécharger plusieurs images et soumettre en add form. Une seule image est enregistrée et uniquement le thumnail est enregistré. Comment obtenir toutes les images enregistrées

+0

Qu'en est-il de votre navigateur? –

Répondre

0

Votre boucle devrait se présenter comme suit:

foreach($_FILES['land_image']['tmp_name'] as $key => $tmp_name) { 
    $fileName = $_FILES['land_image']['name'][$key]; 
    $fileSize = $_FILES['land_image']['size'][$key]; 
    $fileTmp = $_FILES['land_image']['tmp_name'][$key]; 
    $fileType = $_FILES['land_image']['type'][$key]; 
    ... 
} 
0

La réponse est pas pertinente à la question posée. Je vais répondre à ma question:

public function saveAction($lmgdata) 
{ 
    try{ 
     if(isset($_POST['submit'])) 
     { $i = 0; 
     foreach($_FILES as $k=>$v){ 
     // to test is form submited or not 
      $post=$this->_request->getPost(); // get post result 
      $data=array(); // create an array to stroe posted details 
      $data['land_id']=$_SESSION['lid']; 

      //$data['lm_order']=$post['lm_order']; 
      /* below code for create a directory */ 
      $path = "/propladder/public/images/landimages"; 
      $thumb="/propladder/public/images/landimages/thumbnails"; 
      if(!is_dir($path)){ // to check is folder exist or not 
       mkdir($path,'0755',true); 
      } 

      if(!is_dir($thumb)){ // to check is folder exist or not 
       mkdir($thumb,'0755',true); 
      } 
     /* below code for uploading in property image */ 
      $maxWidth="600"; 
      $maxHeidht="500"; 
      list($width,$height,$y) = getimagesize($v['tmp_name'][$i]); 
      $scale = min($maxWidth/$width, $maxHeidht/$height); 

      $thumbwidth="100"; 
      $thumbheidht="75"; 

      $scale1 = min($thumbwidth/$width, $thumbheidht/$height); 

      $s1=$v['name'][$i]; 
      $destpath = $path.'/'.$s1; // this is image fulll path 

      $thumbpath = $thumb.'/'.$s1; // this is image fulll path 

      if(move_uploaded_file($v['tmp_name'][$i],$destpath)){ // to upload image to destination  
       $this->resize($thumbpath, $destpath,$width, $height,$scale1,1); 
       $this->resize($destpath, $destpath,$width, $height,$scale,0); 

       $data['lm_image']=$s1; // to save new image name in db 
      }else{ 
        echo "not uploaded"; die(); 
      } 

      $mapper=new Application_Model_xxxxMapper(); 
      if($mapper->save($data)){ 
       $this->_helper->FlashMessenger(array('success'=>'xxxx added sucessfully')); 
      }else{ 
       $this->_helper->FlashMessenger(array('failed'=>'xxxx not added ,Try again')); 
      } 
      $i++; 
      } 
      } // end is post 
      //$this->_helper->redirector('index','xxxx','admin'); 
     }catch(Zend_Db_Exception $e){ 
      echo $e->getMessage();   
     } 
} 
Questions connexes