2013-04-28 10 views
6

Auch wenn ich 2 oder mehr Bilder auswähle, wird nur eines hochgeladen.Wie lade ich mehrere Dateien mit Zend Framework hoch?

Ich habe eine einfache Form:

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

Da ist in meinem Controller:

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 
      } 
     } 
    } 
} 

und die upload Methode:

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

Irgendwelche Ideen, warum ich nur eine Datei hochgeladen bekommen ?

Antwort

9

Zend_File_Transfer_Adapter_Http hat tatsächlich die Informationen über den Datei-Upload. Sie müssen nur mit dieser Ressource iterieren:

$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

Ich denke, Sie haben ein Problem mit Ihrem Code hier. '$ apt' ist nicht definiert. Meinst du "$ upload"? –

+0

geändert .. Entschuldigung, dass .. – Dinesh

+0

Danke, das hat den Trick für mich für Zf2. –

Verwandte Themen