zendframework 上传图片判断

if($this->_request->isPost()){
           $postData = $this->_request->getPost();
           try{
               $upload = new Zend_File_Transfer_Adapter_Http();
               $files = $upload->getFileInfo();
               $upload->addValidator('Extension', false, array('gif', 'jpeg', 'jpg', 'png'))
                      ->addValidator('Size', false, array('max' => '512kb'));
               $extension = '';
               foreach($files as $_file){
                   if(!empty($_file['name'])){
                        $pathInfo = pathinfo($_file['name']);
                        $extension = $pathInfo['extension'];
                        if(!$upload->isValid($_file['name'])){
                            throw new Exception(implode('\n\r', $upload->getMessages()));
                        }
                    }
               }
               $newFileName = time(). '_'. mt_rand(). '.' . $extension;
               $upload->addFilter('Rename', $newFileName);
               $upload->setDestination(self::RECOMMENDPRODUCT_PATH);
               $upload->receive();
               if(0 < $_FILES['productPic']['size']){
                   //将原有的图片给删除掉
                   if(file_exists(self::RECOMMENDPRODUCT_PATH . $recommendProduct->pic)){
                       unlink(self::RECOMMENDPRODUCT_PATH . $recommendProduct->pic);
                   }
               }
               $filter = new Zend_Filter_StripTags();
               $title = $filter->filter(trim($postData['title']));
               if(empty($title)){
                   throw new Exception('请输入产品名称');
               }
               $url = trim($postData['linkUrl']);
               $status = $postData['status'];

              if(empty($recommendProductId)){
                   $data = array(
                       'title'  => $title,
                       'pic'    => $newFileName,
                       'url'    => $url,
                       'status' => $status
                   );
                   $recommendProductModel->insert($data);
               }else{
                  $editData = array(
                       'title'  => $title,
                       'url'    => $url,
                       'status' => $status
                   );
                   if(0 < $_FILES['productPic']['size']){
                       $editData = array_merge(array('pic' => $newFileName), $editData);
                   }

                   $recommendProductModel->updateById($editData, $recommendProductId);
                   $this->_redirect('/system/edit-recommend-product/id/' . $recommendProductId);
               }
           }catch(Exception $e){
               $this->view->error = $e->getMessage();
           }
       }

 http://hi.baidu.com/xinliang211/item/d404cf49627975eca4c0666f

你可能感兴趣的:(framework)