如何使用Zend Framework 1.12中的单个按钮一次上传多个图像?

时间:2014-04-29 06:46:35

标签: php zend-framework zend-form zend-file

这是我的控制器

图像控制器

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

我在控制器中使用循环来保存多个图像,当我尝试上传多个图像并在add form中提交时。只有1张图像正在保存,而且只保存了thumnail。如何保存所有图像

2 个答案:

答案 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];
    ...
}

答案 1 :(得分: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();           
        }
}