如何上传多个文件?

时间:2011-01-24 10:07:39

标签: php zend-framework

如何告诉控制器或模型这是一个多重上传。所以一个接一个地移动它们。对于单个上传,以下模型有效。

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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;

  }