2017-11-09 1 views
0

을 클릭 한 후 널 (null)이된다 나의 길이다.url 매개 변수가 다음 버튼

<a href="<?php echo $this->url('upload', array('products' =>'shoes')); ?>">Upload Shoes Product Image</a> 
<a href="<?php echo $this->url('upload', array('products' =>'trainers')); ?>">Upload Trainers Product Image</a> 
<a href="<?php echo $this->url('upload', array('products' =>'hats')); ?>">Upload Hat Product Image</a> 

여기 내 컨트롤러 코드입니다. 각 [/:products] 매개 변수에 나는 $fileManagerDir에 주어진 이름을 가진 별도의 폴더를 만들려고 :

<?php 

namespace Products\Controller; 
use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 
use Products\Form\UploadForm; 

class UploadController extends AbstractActionController 
{ 
    protected $_dir = null; 

    public function indexAction() 
    { 
     $products = $this->params()->fromRoute('products'); 

     $config = $this->getServiceLocator()->get('Config'); 
     $fileManagerDir =$config['file_manager']['dir']; 

     $this->_dir = realpath($fileManagerDir) . 
      DIRECTORY_SEPARATOR . 
      $products; 


     if (!is_dir($this->_dir)) { 
      //read, write, execute 
      mkdir($this->_dir, 0777); 
     } 

     $form = new UploadForm($this->_dir, 'upload-form'); 
     $request = $this->getRequest(); 

     if ($request->isPost()) { 
      $post = array_merge_recursive(
       $request->getPost()->toArray(), 
       $request->getFiles()->toArray() 
      ); 

      $form->setData($post); 

      if ($form->isValid()) { 
       $data = $form->getData(); 

       $this->setFileNames($data); 
       return $this->redirect()->toRoute('upload/uploadsuccessful', array('products' =>$products)); 
      } 
     } 
     return new ViewModel(array('form' => $form)); 
    } 

    public function successfulAction() 
    { 
     $file = array(); 
     $flashMessenger = $this->flashMessenger(); 
     if ($flashMessenger->hasMessages()) { 
      foreach($flashMessenger->getMessages() as $key => $value) { 
       $file = $value;  
      } 
     } 
     return new ViewModel(array('file' => $file)); 
    } 

    protected function setFileNames($data) 
    { 
     unset($data['submit']); 
     foreach ($data['image-file'] as $key => $file) { 
      rename($file['tmp_name'], $this->_dir . DIRECTORY_SEPARATOR . $file['name']); 
     }  
    } 
} 

나는 아이디어가 분명하다 생각합니다.

그러나 문제가 있습니다. 버튼 업로드 버튼 ($request->isPost() == true)$products을 클릭하면 null이되고 업로드 된 파일은 적절한 폴더로 이동하지 않습니다. 또한 성공적인 작업으로 리디렉션 할 수 없습니다. $ products가 null이기 때문에 오류가 "누락 된 매개 변수"로 나타납니다.

답변

0

보기 스크립트에서 업로드 버튼은 href 링크입니다. 이렇게하면 GET REQUEST가 POST가 아닌 컨트롤러로 전송되고 FILES도 표시되지 않습니다. 않는 한 여기에서 언급하지 않은 외부 JS로 클릭 이벤트를 가로 채고 있습니다. 경로 URL은 양식 작업 속성에 있어야합니다. 제출 단추는 링크가 아닌 단추 여야합니다 (여기서 언급하지 않은 JS를 사용하지 않는 한). 양식 메소드가 POST로 설정되어 있는지 확인하십시오. 같은

뭔가 :

<form action="<?php echo $this->url('upload', array('products' =>'shoes')); ?>" method="post" enctype="multipart/form-data"> 

당신은 JS를 사용하여 동적 작업을 처리하거나 경로를 재 설계해야합니다.

관련 문제