这里有新鲜出炉的 PHP 教程,程序狗速度看过来!
PHP(外文名: Hypertext Preprocessor,中文名:"超文本预处理器")是一种通用开源脚本语言。语法吸收了 C 语言、Java 和 Perl 的特点,入门门槛较低,易于学习,使用广泛,主要适用于 web 开发领域。PHP 的文件后缀名为 php。
在 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
- }
- }
- }
来源: http://www.phperz.com/article/17/0805/341737.html