分享php多文件上传实现源码

时间:2017-07-27
大家在通过对PHP语言的学习,应该大致都可以掌握文件上传的方法。不过,对于初学者来说,多文件上传还是比较头疼的。下面,我们就为大家介绍关于PHP多文件上传的具体代码。

上传文件代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">     
<html xmlns="http://www.w3.org/1999/xhtml">     
<head>     
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />     
<title>文档上传</title>     
</head>     
<body>     
<mce:script language="javascript"><!--     
 动态添加文件选择控件-->     
   function AddRow()     
   {     
    var eNewRow = tblData.insertRow();     
    for (var i=0;i<1;i++)     
    {     
      var eNewCell = eNewRow.insertCell();     
      eNewCell.innerHTML = "<tr><td><input type='file' name='filelist[]' size='50'/></td></tr>";     
    }     
   }     
         
// --></mce:script>     
<form name="myform" method="post" action="uploadfile.php" enctype="multipart/form-data" >     
  <table id="tblData" width="400" border="0">     
    <!-- 将上传文件必须用post的方法和enctype="multipart/form-data"  -->     
         
    <!-- 将本页的网址传给uploadfile.php-->     
    <input name="postadd" type="hidden" value="<?php echo "http://".$_SERVER['HTTP_HOST'].$_SERVER["PHP_SELF"]; ?>" />     
    <tr><td>文件上传列表          
    <input type="button" name="addfile"  onclick="AddRow()" value="添加列表" /></td></tr>       
   <!-- filelist[]必须是一个数组-->     
   <tr><td><input type="file" name="filelist[]" size="50" /></td></tr>     
        
 </table>     
  <input type="submit" name="submitfile" value="提交文件" />     
 </form>     
</body>     
</html>   

PHP多文件上传之提交文件代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">     
<html xmlns="http://www.w3.org/1999/xhtml">     
<head>     
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />     
<title>文件上传结果</title>     
</head>     
<body>     
<?php     
    if ($_POST["submitfile"]!="")     
       {               
         $Path="./".date('Ym')."/";     
         if (!is_dir($Path))//创建路径     
             {  mkdir($Path);  }      
         echo "<div>";     
         for ($i=0;$i<count($filelist);$i++)     
         { //$_FILES["filelist"]["size"][$i]的排列顺序不可以变,
因为fileist是一个二维数组     
           if ($_FILES["filelist"]["size"][$i]!=0)      
            {     
              $File=$Path.date('Ymdhm')."_".$_FILES["filelist"]["name"][$i];     
              if (move_uploaded_file($_FILES["filelist"]["tmp_name"][$i],$File))     
                 { echo "文件上传成功 文件类型:".$_FILES["filelist"]
["type"][$i]." "."文件名:"    
                        .$_FILES["filelist"]["name"][$i]."<br>"; }     
              else    
                 { echo "文件名:".$_FILES["filelist"]["name"][$i]."上传失败</br>"; }     
            }     
         }     
         echo "</div><br><a href="$postadd" mce_href="$postadd">返回</a></div>";          
       }     
            
   ?>     
</body>     
</html>   

 

move_uploaded_file() 函数讲解

move_uploaded_file() 函数将上传的文件移动到新位置。若成功,则返回 true,否则返回 false。

用法:move_uploaded_file(file,newloc)

  • 参数 file,必需。规定要移动的文件。
  • 参数 newloc,必需。规定文件的新位置。

本函数检查并确保由 file 指定的文件是合法的上传文件(即通过 PHP 的 HTTP POST 上传机制所上传的)。如果文件合法,则将其移动为由 newloc 指定的文件。

如果 file 不是合法的上传文件,不会出现任何操作,move_uploaded_file() 将返回 false。

如果 file 是合法的上传文件,但出于某些原因无法移动,不会出现任何操作,move_uploaded_file() 将返回 false,此外还会发出一条警告。

这种检查显得格外重要,如果上传的文件有可能会造成对用户或本系统的其他用户显示其内容的话。

注释:本函数仅用于通过 HTTP POST 上传的文件。

注意:如果目标文件已经存在,将会被覆盖。