当前位置:Gxlcms > php框架 > php 使用html5实现多文件上传实例

php 使用html5实现多文件上传实例

时间:2021-07-01 10:21:17 帮助过:5人阅读

首先向大家介绍一下html5中file的multiple属性

定义和用法

multiple 属性规定输入字段可选择多个值。如果使用该属性,则字段可接受多个值。

实例:

  1. <form action="demo_form.asp" method="get">
  2. Select images: <input type="file" name="img" multiple="multiple" />
  3. <input type="submit" />
  4. </form>

上面实例中的input file 可接受多个文件上传字段。

了解了html5中file的multiple属性,下面我们开始讲解使用html5实现多文件上传。

实例代码:

html:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. </head>
  6. <body>
  7. <form action="my_parser.php" method="post" enctype="multipart/form-data">
  8. <p><input name="upload[]" type="file" multiple="multiple" /></p>
  9. <input type="submit" value="Upload all files">
  10. </form>
  11. </body>
  12. </html>

php代码:

  1. for($i=0; $i<count($_FILES['upload']['name']); $i++) {
  2. //Get the temp file path
  3. $tmpFilePath = $_FILES['upload']['tmp_name'][$i];
  4. //Make sure we have a filepath
  5. if ($tmpFilePath != ""){
  6. //Setup our new file path
  7. $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];
  8. //Upload the file into the temp dir
  9. if(move_uploaded_file($tmpFilePath, $newFilePath)) {
  10. //Handle other code here
  11. }
  12. }
  13. }

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

人气教程排行