2013-04-28 2 views
6

2 개 이상의 이미지를 선택하더라도 하나만 업로드됩니다. 내 컨트롤러에서 다음Zend 프레임 워크로 여러 파일을 업로드하는 방법은 무엇입니까?

<form action="/images/thumbs" method="post" enctype="multipart/form-data"> 
    <input name="file[]" id="file" type="file" multiple="" /> 
    <input type="submit" name="upload_images" value="Upload Images"> 
</form> 

:

public function thumbsAction() 
{ 
    $request = $this->getRequest(); 

    if ($request->isPost()) { 
     if (isset($_POST['upload_images'])) { 
      $names = $_FILES['file']['name']; 

      // the names will be an array of names 
      foreach($names as $name){ 
       $path = APPLICATION_PATH.'/../public/img/'.$name; 
       echo $path; // will return all the paths of all the images that i selected 
       $uploaded = Application_Model_Functions::upload($path); 
       echo $uploaded; // will return true as many times as i select pictures, though only one file gets uploaded 
      } 
     } 
    } 
} 

upload 방법 : 난 단지 하나 개의 파일을받을 이유

public static function upload($path) 
{ 
    $upload = new Zend_File_Transfer_Adapter_Http(); 
    $upload->addFilter('Rename', array(
     'target' => $path, 
     'overwrite' => true 
    )); 

    try { 
     $upload->receive(); 
     return true; 
    } catch (Zend_File_Transfer_Exception $e) { 
     echo $e->message(); 
    } 
} 

어떤 아이디어가 업로드

나는 간단한 양식을 ?

답변

9

Zend_File_Transfer_Adapter_Http에는 실제로 파일 업로드에 대한 정보가 있습니다. 해당 리소스를 사용하여 반복해야합니다.

$upload = new Zend_File_Transfer_Adapter_Http(); 
$files = $upload->getFileInfo(); 
foreach($files as $file => $fileInfo) { 
    if ($upload->isUploaded($file)) { 
     if ($upload->isValid($file)) { 
      if ($upload->receive($file)) { 
       $info = $upload->getFileInfo($file); 
       $tmp = $info[$file]['tmp_name']; 
       // here $tmp is the location of the uploaded file on the server 
       // var_dump($info); to see all the fields you can use 
      } 
     } 
    } 
} 
+1

여기 코드에 문제가 있다고 생각합니다. '$ apt'가 정의되지 않았습니다. '$ upload'를 원하셨습니까? –

+0

변경 .. 죄송합니다 .. – Dinesh

+0

감사합니다. Zf2의 트릭입니다. –

관련 문제