html簡單頁面:
index.html代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<form action= "{:u('index/upload')}" method= "post" enctype= "multipart/form-data" > 文件上傳:<input type= "file" name = "test[]" > 文件上傳:<input type= "file" name = "test[]" > 文件上傳:<input type= "file" name = "test[]" > 文件上傳:<input type= "file" name = "test[]" > 文件上傳:<input type= "file" name = "test[]" > 文件上傳:<input type= "file" name = "test[]" > 文件上傳:<input type= "file" name = "test[]" > 文件上傳:<input type= "file" name = "test[]" > 文件上傳:<input type= "file" name = "test[]" > 文件上傳:<input type= "file" name = "test[]" > 文件上傳:<input type= "file" name = "test[]" > 文件上傳:<input type= "file" name = "test[]" > <input type= "submit" value = "提交" > </form> |
控制器indexcontroller.class.php代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?php namespace home\controller; use think\controller; class indexcontroller extends controller { public function index(){ $this ->display(); } public function upload(){ if (is_post){ $config = array ( 'maxsize' => 3145728, 'rootpath' => './uploads/' , 'savepath' => '' , 'savename' => array ( 'uniqid' , mt_rand(1,999999). '_' .md5(uniqid())), 'exts' => array ( 'jpg' , 'gif' , 'png' , 'jpeg' ), 'autosub' => true, 'subname' => array ( 'date' , 'ymd' ), ); $upload = new \think\upload( $config ); // 實(shí)例化上傳類 $info = $upload ->upload(); if (! $info ) { $this ->error( $upload ->geterror()); } else { foreach ( $info as $file ){ echo $file [ 'savepath' ]. $file [ 'savename' ]; } } } else { $this ->display(); } } } |
上傳結(jié)果顯示:
好多人在進(jìn)行多文件上傳的時候,最后發(fā)現(xiàn)只是上傳了一張,主要就是命名所致,因?yàn)槭峭瑯拥拿?,所以最后就剩一張圖片
解決方法:第一種:
1
2
3
4
5
6
7
8
|
$config = array ( 'maxsize' => 3145728, 'rootpath' => './uploads/' , 'exts' => array ( 'jpg' , 'gif' , 'png' , 'jpeg' ), 'autosub' => true, 'subname' => array ( 'date' , 'ymd' ), 'saverule' => '' , ); |
置空$config里面的saverule,上傳后的名稱為:59c8d38cdb968.jpg
若是感覺這種命名不可靠,可采取第二種方法:
1
2
3
4
5
6
7
8
|
$config = array ( 'maxsize' => 3145728, 'rootpath' => './uploads/' , 'savename' => array ( 'uniqid' , mt_rand(1,999999). '_' .md5(uniqid())), 'exts' => array ( 'jpg' , 'gif' , 'png' , 'jpeg' ), 'autosub' => true, 'subname' => array ( 'date' , 'ymd' ), ); |
設(shè)置$config中: 'savename' => array('uniqid', mt_rand(1,999999).'_'.md5(uniqid())),
其最后的結(jié)果類似于:672563_30ad4d8a2aafc832363de8edc1940b5c59c8d44a303f9.jpg
然,命名可根據(jù)需要自行修改,多文件上傳方法很多,這里只是提供個簡單便捷的方法!
以上這篇thinkphp3.2簡單解決多文件上傳只上傳一張的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/zfy0818/p/7593268.html