【问题标题】:How to upload Multiple images at a time with single button in Zend Framework 1.12?如何在 Zend Framework 1.12 中使用单个按钮一次上传多个图像?
【发布时间】:2014-04-29 06:46:35
【问题描述】:

这是我的控制器

图像控制器

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

我在控制器中使用 for 循环来保存多张图片,当我尝试上传多张图片并在 add form 中提交时。只有 1 张图像正在保存,也只有缩略图被保存。如何保存所有图像

【问题讨论】:

  • 你的浏览器呢?

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


【解决方案1】:

您的循环应如下所示:

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

【讨论】:

    【解决方案2】:

    答案与提出的问题无关。我会回答我的问题:

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

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 2014-04-08
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      • 2013-07-05
      • 1970-01-01
      相关资源
      最近更新 更多