在php开发应用中经常会碰到文件上传,有时也会碰到要多文件上传,下面我们就来看看我提供的三款php多文件上传实例代码,好了费话不说多了来看看这些多文件上传功能适合你么.
示例代码如下:
- <form action="" method="post" enctype="multipart/form-data" >
- <input type="file" name="uploadfile[]"> 命名必是这样有"[]"
- <input type="file" name="uploadfile[]">
-
-
- $type = array('gif', 'jpg', 'png', 'zip', 'rar');
- $upload = new uploadfile($_files['uploadfile'], '/', 1024*1024, $type);
- 参数说明:1:表单的文件,2:上传目录,3:支持文件大小,4:允许文件类型
- $icount = $upload->upload();
- if($icount > 0) {
- print_r($upload->getsaveinfo());
- */
-
- class uploadfile {
- var $postfile = array();
- var $custompath = "";
- var $maxsize = "";
- var $lasterror = "";
- var $allowtype = array('gif', 'jpg', 'png', 'zip', 'rar', 'txt', 'doc', 'pdf');
- var $endfilename = "";
- var $saveinfo = array();
- var $root_dir = "";
-
-
-
-
-
-
- function uploadfile($arrfile, $path="_", $size = 2097152, $type = 0) {
- $this->postfile = $arrfile;
- $this->custompath = $path == "_" ? "" : $path ;
- $this->maxsize = $size;
- if($type!=0) $this->allowtype = $arrfile;
- $this->root_dir = $_server['document_root'];
- $this->_mkdir($this->custompath);
- }
-
-
-
-
-
-
-
- function upload() {
- $ilen = sizeof($this->postfile['name']);
- for($i=0;$i<$ilen;$i++){
- if ($this->postfile['error'][$i] == 0) {
-
- $sname = $this->postfile['name'][$i];
- $stname = $this->postfile['tmp_name'][$i];
- $isize = $this->postfile['size'][$i];
- $stype = $this->postfile['type'][$i];
- $sextn = $this->_getextname($sname);
-
-
-
- if($this->_checksize){
- $this->lasterror = "您上传的文件[".$sname."],超过系统支持大小!";
- $this->_showmsg($this->lasterror);
- continue;
- }
-
- if(!is_uploaded_file($stname)) {
- $this->lasterror = "您的文件不是通过正常途径上传!";
- $this->_showmsg($this->lasterror);
- continue;
- }
- $_filename = basename($sname,".".$sextn)."_".time().".".$sextn;
- $this->endfilename = $this->custompath.$_filename;
-
- if(!move_uploaded_file($stname, $this->root_dir.$this->endfilename)) {
- $this->lasterror = $this->postfile['error'][$i];
- $this->_showmsg($this->lasterror);
- continue;
- }
-
-
- $this->save_info[] = array("name" => $sname, "type" => $sextn, "size" => $isize, "path" => $this->endfilename);
- }
- }
-
- return sizeof($this->save_info);
- }
-
-
-
-
-
- function getsaveinfo(){
- return $this->save_info;
- }
-
-
-
-
-
-
-
-
-
- private function _getextname($filename){
- $arrparts = pathinfo($filename);
- return $arrparts['extension'];
- }
-
-
-
-
-
-
- private function _checksize($size){
- return $size > $this->maxsize;
- }
-
-
-
-
-
-
- private function _showmsg($msg){
- printf("<b><错误信息:></b> %s <br>n", $msg);
- }
-
-
-
-
-
-
- private function _mkdir($p){
- $ap = split('[/]', $p);
- foreach($ap as $v){
- if(!emptyempty($v)){
- if(emptyempty($path)) $path=$v;
- else $path.='/'.$v;
- file_exists($this->root_dir."/".$path) or mkdir($this->root_dir."/".$path);
- }
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class uploadfile {
-
- var $user_post_file = array();
- var $save_file_path;
- var $max_file_size;
- var $last_error;
-
- var $allow_type = array('gif', 'jpg', 'png', 'zip', 'rar', 'txt', 'doc', 'pdf');
- var $final_file_path;
-
- var $save_info = array();
-
-
-
-
-
-
-
-
-
- function uploadfile($file, $path, $size = 2097152, $type = '') {
- $this->user_post_file = $file;
- $this->save_file_path = $path;
- $this->max_file_size = $size;
- if ($type != '')
- $this->allow_type = $type;
- }
-
-
-
-
-
-
- function upload() {
-
- for ($i = 0; $i < count($this->user_post_file['name']); $i++) {
-
- if ($this->user_post_file['error'][$i] == 0) {
-
- $name = $this->user_post_file['name'][$i];
- $tmpname = $this->user_post_file['tmp_name'][$i];
- $size = $this->user_post_file['size'][$i];
- $mime_type = $this->user_post_file['type'][$i];
- $type = $this->getfileext($this->user_post_file['name'][$i]);
-
- if (!$this->checksize($size)) {
- $this->last_error = "the file size is too big. file name is: ".$name;
- $this->halt($this->last_error);
- continue;
- }
-
- if (!$this->checktype($type)) {
- $this->last_error = "unallowable file type: .".$type." file name is: ".$name;
- $this->halt($this->last_error);
- continue;
- }
-
- if(!is_uploaded_file($tmpname)) {
- $this->last_error = "invalid post file method. file name is: ".$name;
- $this->halt($this->last_error);
- continue;
- }
-
- $basename = $this->getbasename($name, ".".$type);
-
- $saveas = $basename."-".time().".".$type;
-
- $this->final_file_path = $this->save_file_path."/".$saveas;
- if(!move_uploaded_file($tmpname, $this->final_file_path)) {
- $this->last_error = $this->user_post_file['error'][$i];
- $this->halt($this->last_error);
- continue;
- }
-
- $this->save_info[] = array("name" => $name, "type" => $type,
- "mime_type" => $mime_type,
- "size" => $size, "saveas" => $saveas,
- "path" => $this->final_file_path);
- }
- }
- return count($this->save_info);
- }
-
-
-
-
-
-
- function getsaveinfo() {
- return $this->save_info;
- }
-
-
-
-
-
-
-
- function checksize($size) {
- if ($size > $this->max_file_size) {
- return false;
- }
- else {
- return true;
- }
- }
-
-
-
-
-
-
- function checktype($extension) {
- foreach ($this->allow_type as $type) {
- if (strcasecmp($extension , $type) == 0)
- return true;
- }
- return false;
- }
-
-
-
-
-
-
- function halt($msg) {
- printf("<b><uploadfile error:></b> %s <br>n", $msg);
- }
-
-
-
-
-
-
-
- function getfileext($filename) {
- $stuff = pathinfo($filename);
- return $stuff['extension'];
- }
-
-
-
-
-
-
-
-
- function getbasename($filename, $type) {
- $basename = basename($filename, $type);
- return $basename;
- }
- }
-
-
-
-
- ?>
一个简单实例,代码如下:
- <form enctype="multipart/form-data" action="up.php" method="post">
- send this file: <input name="userfile[]" type="file" /><br>
- send this file: <input name="userfile[]" type="file" /><br>
- send this file: <input name="userfile[]" type="file" /><br>
- <input type="submit" value="send file" />
- </form>
-
-
- <?php
-
-
-
- $uploaddir = './';
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ?>
-
-
- <?php
-
- function rearrayfiles(&$file_post) {
-
- $file_ary = array();
- $file_count = count($file_post['name']);
- $file_keys = array_keys($file_post);
-
- for ($i=0; $i<$file_count; $i++) {
- foreach ($file_keys as $key) {
- $file_ary[$i][$key] = $file_post[$key][$i];
- }
- }
-
- return $file_ary;
- }
-
-
- print "<pre>";
-
- if ($_files['userfile']) {
- $file_ary = rearrayfiles($_files['userfile']);
|