php语言 百分网手机站

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

时间:2020-09-29 08:34:31 php语言 我要投稿

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

  在html没有出来之前,要实现php多文件上传比较麻烦,需要在form表单里面添加多个input file域。html5发布以后,我们可以使用input file的html5属性multiple来实现多文件上传,需要的.朋友可以参考下。

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

  定义和用法:

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

  实例:

  <form action="demo_form.asp" method="get">

  Select images: <input type="file" name="img" multiple="multiple" />

  <input type="submit" />

  </form>

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

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

  实例代码:

  html:

  <!DOCTYPE html>

  <html>

  <head>

  <meta charset="UTF-8">

  </head>

  <body>

  <form action="my_parser.php" method="post" enctype="multipart/form-data">

  <p><input name="upload[]" type="file" multiple="multiple" /></p>

  <input type="submit" value="Upload all files">

  </form>

  </body>

  </html>

  php代码:

  for($i=0; $i<count($_FILES['upload']['name']); $i++) {

  //Get the temp file path

  $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

  //Make sure we have a filepath

  if ($tmpFilePath != ""){

  //Setup our new file path

  $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];

  //Upload the file into the temp dir

  if(move_uploaded_file($tmpFilePath, $newFilePath)) {

  //Handle other code here

  }

  }

  }

  最后,非常感谢大家的阅读,希望能帮助到大家!

【php使用html5实现多文件上传实例】相关文章:

PHP实现大文件上传源代码07-31

PHP文件上传源码分析08-22

PHP+jQuery+Ajax仿淘宝多上传按钮单文件上传06-17

PHP中读取大文件实现方法08-27

PHP中读取大文件实现方法详解10-29

PHP文件锁与进程锁的实现06-20

PHP时间戳使用实例代码「详细版」06-03

PHP文件是什么 如何打开PHP文件07-26

php如何使用fgetcsv读取csv文件出现乱码11-16

PHP中使用crypt()实现用户身份验证07-26