1、为什么不能自动感知file对象名而需要显式的传递呢?
2、如果file对象不以数组方式命名,则
foreach ($_FILES[$this->filename]['name'] as $key=>$fname)
将出错!!!

解决方案 »

  1.   

    uploadMuti,多文件上传
    新增方法
    uploadSingle,单文件上传修改后的版本。<style>
    body{font:12px verdana;}
    </style>
    <pre> 
    <?php
    // 在 4.1.0 以前的 PHP 中,需要用 $HTTP_POST_FILES 代替 $_FILES。
    // 在 4.0.3 以前的 PHP 中,需要用 copy() 和 is_uploaded_file() 来代替 move_uploaded_file()。
    /**
     * Short description. fileupload
     *
     * Detail description fileupload
     * @author       
     * @version      1.0
     * @copyright [email protected]    
     */
    class fileupload
    {
    var  $uploaddir ;
    var  $filename = 'userfile';
    var  $maxsize = 2048888;
    var  $extname = 'html,txt,htm';function setdir(){
    $uploaddir = $this->uploaddir;
    if (!is_dir($uploaddir)) {
        $this->mkdirp($uploaddir);
    }
    }//end func setdirfunction uploadSingle(){
    $fname = $_FILES[$this->filename]['name'];
    if ($this->checkFileExt($fname)) {
    $fsize = $_FILES[$this->filename]['size'];
    if ($fsize <$this->maxsize) {
    $uploadfile = $this->uploaddir . $this->filename;
    $tmpfile = $_FILES[$this->filename]['tmp_name'];
    echo "$uploadfile = $tmpfile ".$fsize."\n";
    if (move_uploaded_file($tmpfile, $uploadfile)) {
    print "$fname File is valid, and was successfully uploaded.\n";
    } else {
    print "Possible file upload attack!  Here's some debugging info:\n";
    print_r($_FILES);
    }
    }//size end
    else {
    $fsize /=1024*1024; 
        echo "$fname size is $fsize MB:the max upload size is 1MB";
    }
    }//fname}//end func uploadSingle/**
     * Short description.  
     * @param      none
    */
    function uploadMulti ()
    {
    foreach ($_FILES[$this->filename]['name'] as $key=>$fname) {
    if ($this->checkFileExt($fname)) {
    $fsize = $_FILES[$this->filename]['size'][$key];
    if ($fsize <$this->maxsize) {
    $uploadfile = $this->uploaddir . $fname;
    $tmpfile = $_FILES[$this->filename]['tmp_name'][$key];
    echo "$uploadfile = $tmpfile ".$fsize."\n";
    if (move_uploaded_file($tmpfile, $uploadfile)) {
    print "$fname File is valid, and was successfully uploaded.\n";
    //print_r($_FILES);
    } else {
    print "Possible file upload attack!  Here's some debugging info:\n";
    print_r($_FILES);
    }
    }//size end
    else {
    $fsize /=1024*1024; 
        echo "$fname size is $fsize MB:the max upload size is 1MB";
    }
    }//fname
    }//for
    } // end func uploadMultifunction checkFileExt($fname){
    $path_parts = pathinfo($fname);
    $ext = $path_parts["extension"];
    $re = "/$ext/i";
    preg_match($re,$this->extname, $matches);if ($matches[0]) //是文本文件扩展名为:.php,.xml,.css,.js.......由数组$matches定义
    {
    //echo $path_parts["dirname"] . "\n" .$path_parts["basename"]. "\n".$path_parts["extension"] . "\n";
    return true;
    }
    return false;
    }// end func/** 
    * mkdirp is used to instead of mkdir ,mkdirp can create deep multiple directory.
    */
    function mkdirp($target) {
      // If the path already exists && is a directory, all is well.
      // If the path is not a directory, we've a problem.
      if (file_exists($target)) {
       if (!is_dir($target)) return false;
       else return true;
      }  // Attempting to create the directory may clutter up our display.
      if ( @mkdir($target) ) return true;  // If the above failed, attempt to create the parent node, then try again.
      if ( $this->mkdirp(dirname($target)) ) return $this->mkdirp($target);  return false;
    }//</function mkdirp>} // end classif ($_FILES['userfile']<>'') {
        $mu = new fileupload;
    $mu->filename = 'userf';
    $mu->extname = 'html,htm,txt,zip,rar';
    $mu->maxsize = 1*1024*1024;
    $mu->uploaddir = 'd:/usr/www/uploads/';
    $mu->uploadSingle();
    }
    ?> 
    <FORM METHOD=POST enctype="multipart/form-data" ACTION="?">
    <input type="file" name="userfile[]"  />
    <input type="file" name="userfile[]"  />
    <input type="file" name="userfile[]"  />
    s<input type="file" name="userf"  />
    <input type="submit" value="submit" />
    </FORM>
      

  2.   

    http://blog.csdn.net/kingerq/archive/2005/06/17/396363.aspx这个怎么样??多文件和单文件都可以的哦。。
      

  3.   

    分开清晰点,改起来方便点,根据两位的建议,改了下,上传用同一个函数 upload(),可自行判断是否为多文件上传。
    editplus里编辑,演示+类代码共140行,4KB,功能上大家还有什么意见?<style>
    body{font:12px verdana;}
    </style>
    <pre> 
    <?php
    // 在 4.1.0 以前的 PHP 中,需要用 $HTTP_POST_FILES 代替 $_FILES。
    // 在 4.0.3 以前的 PHP 中,需要用 copy() 和 is_uploaded_file() 来代替 move_uploaded_file()。
    /**
     * Short description. fileupload
     * @author gu1dai       
     * @version     0.1
     * @copyright [email protected]   
     */
    class fileupload
    {
    var  $uploaddir ;
    var  $filename = 'userfile';
    var  $maxsize = 2048888;
    var  $extname = 'html,txt,htm';function setdir(){
    $uploaddir = $this->uploaddir;
    if (!is_dir($uploaddir)) {
        $this->mkdirp($uploaddir);
    }
    }//end func setdirfunction upload(){
    if (is_array($_FILES[$this->filename]['name'])) {
    $this->uploadMulti();    
    echo 'multi upload';
    }
    else {
    $this->uploadSingle();
    echo 'single upload';
    }
    }function uploadSingle(){
    $fname = $_FILES[$this->filename]['name'];
    if ($this->checkFileExt($fname)) {
    $fsize = $_FILES[$this->filename]['size'];
    if ($fsize <$this->maxsize) {
    $uploadfile = $this->uploaddir . $this->filename;
    $tmpfile = $_FILES[$this->filename]['tmp_name'];
    echo "$uploadfile = $tmpfile ".$fsize."\n";
    if (move_uploaded_file($tmpfile, $uploadfile)) {
    print "$fname File is valid, and was successfully uploaded.\n";
    } else {
    print "Possible file upload attack!  Here's some debugging info:\n";
    print_r($_FILES);
    }
    }//size end
    else {
    $fsize /=1024*1024; 
        echo "$fname size is $fsize MB:the max upload size is 1MB";
    }
    }//fname}//end func uploadSingle/**
     * Short description. uploadMulti is used to upload lots of files 
    */
    function uploadMulti ()
    {
    foreach ($_FILES[$this->filename]['name'] as $key=>$fname) {
    if ($this->checkFileExt($fname)) {
    $fsize = $_FILES[$this->filename]['size'][$key];
    if ($fsize <$this->maxsize) {
    $uploadfile = $this->uploaddir . $fname;
    $tmpfile = $_FILES[$this->filename]['tmp_name'][$key];
    echo "$uploadfile = $tmpfile ".$fsize."\n";
    if (move_uploaded_file($tmpfile, $uploadfile)) {
    print "$fname File is valid, and was successfully uploaded.\n";
    //print_r($_FILES);
    } else {
    print "Possible file upload attack!  Here's some debugging info:\n";
    print_r($_FILES);
    }
    }//size end
    else {
    $fsize /=1024*1024; 
        echo "$fname size is $fsize MB:the max upload size is 1MB";
    }
    }//fname
    }//for
    } // end func uploadMultifunction checkFileExt($fname){
    $path_parts = pathinfo($fname);
    $ext = $path_parts["extension"];
    $re = "/$ext/i";
    preg_match($re,$this->extname, $matches);if ($matches[0]) //是文本文件扩展名为:.php,.xml,.css,.js.......由数组$matches定义
    {
    return true;
    }
    return false;
    }// end func/** 
    * mkdirp is used to instead of mkdir ,mkdirp can create deep multiple directory.
    */
    function mkdirp($target) {
      // If the path already exists && is a directory, all is well.
      // If the path is not a directory, we've a problem.
      if (file_exists($target)) {
       if (!is_dir($target)) return false;
       else return true;
      }  // Attempting to create the directory may clutter up our display.
      if ( @mkdir($target) ) return true;  // If the above failed, attempt to create the parent node, then try again.
      if ( $this->mkdirp(dirname($target)) ) return $this->mkdirp($target);  return false;
    }//</function mkdirp>} // end classif ($_FILES['userfile']<>'') {
        $mu = new fileupload;
    $mu->filename = 'userf';
    $mu->extname = 'html,htm,txt,zip,rar';
    $mu->maxsize = 1*1024*1024;
    $mu->uploaddir = 'd:/usr/www/uploads/';
    $mu->upload();
    }
    ?> 
    <FORM METHOD=POST enctype="multipart/form-data" ACTION="?">
    <input type="file" name="userfile[]"  />
    <input type="file" name="userfile[]"  />
    <input type="file" name="userfile[]"  />
    s<input type="file" name="userf"  />
    <input type="submit" value="submit" />
    </FORM>
      

  4.   

    不需要那么麻烦吧?
      function savefile($ar) {
       //检查和保存上传文件,代码略
      }
      function upload() {
    //检查是否有上传对象
    if(! isset($_FILES))
    return;
    //自动感知file对象的名字,无须由外部提供
    foreach($_FILES as $file=>$info) {
    //如果是独立对象则直接处理
    if(! is_array($info['name'])) {
    $this->savefile($info);
    continue;
    }
    //否则提取相应的元组进行处理
    for($i=0;$i<count($info['name']);$i++) {
    $this->savefile(array(
    'name' => $info['name'][$i],
    'type' => $info['type'][$i],
    'tmp_name' => $info['tmp_name'][$i],
    'error' => $info['error'][$i],
    'size' => $info['size'][$i],
    ));
    }
    }
      }
      

  5.   

    我的意思是实现表单自动填写?---------
    用script给表单的file项给予值应该就可以了
    类似form.userfile1.value='d:/web/xxx.zip';
    form.submit();