2011-01-24 62 views
0

如何判斷這是多重上傳的控制器或型號。所以一個一個地移動它們。爲單一上傳以下模型作品。如何上傳多個文件?

// html 
    <input type=file name=moreattachment[] /> 
    <input type=file name=moreattachment[] /> 

    /* Controller */ 
    $file = $_FILES['moreattachment']['name']; 
    foreach($_FILES['moreattachment']['name'] as $k) 
    { 
    Zend_Debug::dump($k); 
    //Application_Model_File::mvFile(); // let us move 1 by 1 
    } 



    /* Model - how do you handle the moreattachment[] ? */ 
    public static function mvFile() 
    { 

    $fname = basename($_FILES['moreattachment']['name']); 
    $_fname = strtolower (end(explode('.',$fname))); 

    switch($_fname) 
    { 
     case ($_fname == 'jpg' || 
      $_fname == 'jpeg' || 
      $_fname == 'gif' || 
      $_fname == 'bmp' || 
      $_fname == 'png' || 
      $_fname == 'html' || 
      $_fname == 'pdf' || 
      $_fname == 'doc' || 
      $_fname == 'docx' || 
      $_fname == 'xls' 
     ): 
     $target_path = APPLICATION_PATH . "/../public/files/attachment/"; 
     //chmod("../up" , 0777); 

     $target_path = $target_path . basename($_FILES['moreattachment']['name']); 

     if(move_uploaded_file($_FILES['moreattachment']['tmp_name'], $target_path)) { 

     }else{ 

     } 
     break; 
    } 


    return $fname; 

    } 



output of $_FILE 
    ["moreattachment"] => array(5) { 
    ["name"] => array(2) { 
     [0] => string(4) "celt" 
     [1] => string(4) "celt" 
    } 
    ["type"] => array(2) { 
     [0] => string(24) "application/octet-stream" 
     [1] => string(24) "application/octet-stream" 
    } 
    ["tmp_name"] => array(2) { 
     [0] => string(14) "/tmp/phpf9QqF0" 
     [1] => string(14) "/tmp/php4NLRLu" 
    } 
    ["error"] => array(2) { 
     [0] => int(0) 
     [1] => int(0) 
    } 
    ["size"] => array(2) { 
     [0] => int(11238) 
     [1] => int(11238) 
    } 
    } 
+0

uploadify.com ..;) – Calum 2011-01-24 10:59:04

+0

那豈不是更容易使用Zend_Form和Zend_Form_Element_File元素來處理您的上傳? – Marcin 2011-01-24 11:27:37

回答

0

希望幫助別人多文件上傳+字數。這回答了我的問題。

Output: 
array(2) { 
    [0] => array(2) { 
    ["filename"] => string(25) "lgo2_nobaseline_large.png" 
    ["size"] => string(2) "0 
" 
    } 
    [1] => array(2) { 
    ["filename"] => string(10) "index.html" 
    ["size"] => string(2) "0 
" 
    } 
} 


    // controller  
    public function stepaAction() 
    { 

     $post = $this->getRequest()->getPost(); 

     $i = 0; 
     $files = array(); 
     foreach($_FILES['moreattachment']['name'] as $k) 
     { 
      //Zend_Debug::dump($_FILES['moreattachment']['name'][$i]); 
      $files[] = array( 
         'filename' => $_FILES['moreattachment']['name'][$i], 
         'size'  => Application_Model_Db::mvMultiFile($i), 
      ); 
      $i++; 
     } 

     Zend_Debug::dump($files); 
     } 

    // model 
    /* Multi attachment files */ 
    public static function mvMultiFile($whichone) 
    { 

    $fname = basename($_FILES['moreattachment']['name'][$whichone]); 
    $_fname = strtolower (end(explode('.',$fname))); 

    switch($_fname) 
    { 
     case ($_fname == 'jpg' || 
      $_fname == 'jpeg' || 
      $_fname == 'gif' || 
      $_fname == 'bmp' || 
      $_fname == 'png' || 
      $_fname == 'html' || 
      $_fname == 'pdf' || 
      $_fname == 'doc' || 
      $_fname == 'docx' || 
      $_fname == 'xls' 
     ): 
     $target_path = APPLICATION_PATH . "/../public/files/attachment/"; 
     //chmod("../up" , 0777); 

     $target_path = $target_path . basename($_FILES['moreattachment']['name'][$whichone]); 

     if(move_uploaded_file($_FILES['moreattachment']['tmp_name'][$whichone], $target_path)) 
     { 

      switch($_fname) 
      { 
      case 'pdf': 
       // convert the pdf to text and then count it 
       $tmpfile = $target_path . $fname . '.txt'; 
       $wc = shell_exec("pdftotext $fileAndPath $tmpfile;wc -w $tmpfile"); 
       $wc = explode(" ", $wc); 
       $result = $wc[0]; 
       break; 

      case 'tiff': 
       $tmpfile = $target_path . $fname . '.pdf'; 
       $tmptxt = $target_path . $fname . '.txt'; 
       $wc = shell_exec("convert $fileAndPath $tmpfile;pdftotext $tmpfile $tmptxt;wc -w $tmptxt"); 
       $wc = explode(" ", $wc); 
       $result = $wc[0]; 
       break; 

      default: 
       //shell_exec("\cp -frR $multisource $usb;du -hs $usb;df -h"); 
       $wc = shell_exec("wc -w $fileAndPath"); 
       $wc = explode(" ", $wc); 
       $result = $wc[0]; 
       break; 

      } 


     }else{ 

     } 
     break; 
    } 


    //return $fname; 
    return $result; 

    } 
0
/* Controller */ 
    foreach($_FILES['moreattachment'] as $f) 
    { 
    $k = $f['name']; 
    Zend_Debug::dump($k); 
    //Application_Model_File::mvFile(); // let us move 1 by 1 
    }