当前位置:首页 > PHP教程 > PHP常见问题

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

首先向大家介绍一下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 } } }

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



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!