2014-04-29 22 views
0

這是我的控制器如何使用Zend Framework 1.12中的單個按鈕一次上傳多個圖像?

圖像打印控制器

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(); 

      } 
} 

這是我的外接形式,我上傳多張圖片 添加形式:

<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=""> 

我用於循環我n控制器保存多個圖像並嘗試上傳多個圖像並在add form中提交。只有1張圖像正在保存,並且只保存了thumnail。如何獲得所有圖像保存

+0

你的瀏覽器怎麼樣? –

回答

0

你的循環應該如下所示:

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

答案是不相關的問這個問題。我會回答我的問題:

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();   
     } 
} 
相關問題