多文件上传其实就包括了图片及各种文件了,下面介绍的是一款PHP多文件上传类,一共两个文件,upp.php 和 uploadFile.php,upp.php,这是前台所显示的表单文件了,默认的是四个上传文件域,我们可以手动进行修改,另外这个页面嵌套了 uploadFile.php 文件上传类,下面一起来看例子.
php文件上传例子,代码如下:
- <?php
- header('content-type:text/html;charset=utf-8');
- require('uploadFile.php');
- if(isset($_POST['submit'])){
- $uploads = $_FILES['file'];
- $num_file = count($uploads['name']);
- $up = new UploadFile($uploads,'uploads',1024);
- $num = $up->upload();
- if($num == $num_file ){
- echo '全部文件上传成功';
- exit;
- }else{
- echo $num,'个文件上传成功<br/>';
- echo $up->showErrorInfo();
- exit;
- }
- }
- ?>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.111cn.net/ 1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- </head>
- <body>
- <form action="uup.php" method="post" enctype="multipart/form-data">
- <p><input name="file[]" type="file" /></p>
- <p><input name="file[]" type="file" /></p>
- <p><input name="file[]" type="file" /></p>
- <p><input name="file[]" type="file" /></p>
- <input name="submit" type="submit" />
- </form>
- </body>
- </html>
PHP文件上传类代码,代码如下:
- <?php
-
- class UploadFile
- {
- var $user_post_file = array();
- var $save_file_path = '';
- var $max_file_size = '';
- var $allow_type = array('gif','jpg','png','zip','rar','txt','doc','pdf');
- var $final_file_path = '';
- var $save_info = array();
- var $error_info = array();
-
-
-
-
-
-
-
-
- function __construct($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];
- $tmp_name = $this->user_post_file['tmp_name'][$i];
- $size = $this->user_post_file['size'][$i];
- $type = $this->user_post_file['type'][$i];
- $ext_name = $this->getExtName($name);
-
- if(!$this->checkSize($size)){
- $this->error_info[] = '您上传的文件:'.$name.'太大';
- continue;
- }
-
- if(!$this->checkType($ext_name)){
- $this->error_info[] = '您上传的文件:'.$name.'不合法';
- continue;
- }
-
- if(!is_uploaded_file($tmp_name)){
- $this->error_info[] = '您上传的文件:'.$name.'属于非法提交';
- continue;
- }
-
- $basename = $this->getBaseName($name,".".$ext_name);
- $final_filename = $basename.'-'.time().'-'.rand(1,10000).'.'.$ext_name;
- $this->final_file_path = $this->save_file_path.'/'.$final_filename;
- if(!move_uploaded_file($tmp_name,$this->final_file_path)){
- $this->error_info = $this->user_post_file['error'][$i];
- continue;
- }
-
- $this->save_info[] = array(
- "name" => $name,
- "ext_name" => $ext_name,
- "type" => $type,
- "size" => $size,
- "final_filename" => $final_filename,
- "path" => $this->final_file_path
- );
- }
- }
- return count($this->save_info);
- }
-
-
-
-
-
-
-
- function checkSize($size)
- {
- if($size > $this->max_file_size){
- return FALSE;
- }
- return TRUE;
- }
-
-
-
-
-
-
- function checkType($extension)
- {
- foreach($this->allow_type as $type){
- if(strcasecmp($extension,$type) == 0){
- return TRUE;
- }
- }
- return FALSE;
- }
-
-
-
-
-
-
-
- function getExtName($filename)
- {
- $p = pathinfo($filename);
- return $p['extension'];
- }
-
-
-
-
-
-
-
-
- function getBaseName($filename,$ext_name)
- {
- $basename = basename($filename,$ext_name);
- return $basename;
- }
-
-
-
-
-
- function showErrorInfo()
- {
- if(count($this->error_info) != 0){
-
- foreach($this->error_info as $k=>$v){
- echo ($k+1),':',$v,'<br/>';
- }
- }
- }
- function getSaveInfo()
- {
- return $this->save_info;
- }
- }
-
-
-
- ?>
|