2013-04-28 6 views
6

भले ही मैं 2 या अधिक छवियों का चयन करता हूं, केवल एक अपलोड हो जाता है।ज़ेंड फ्रेमवर्क के साथ कई फाइलें कैसे अपलोड करें?

<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' परिभाषित नहीं किया गया है। क्या आपका मतलब '$ अपलोड' था? –

+0

बदल गया .. इसके बारे में क्षमा करें .. – Dinesh

+0

धन्यवाद, यह मेरे लिए Zf2 के लिए चाल है। –

संबंधित मुद्दे