<?php

class Upload{
var $saveName;// 保存名
var $savePath;// 保存路径
var $fileFormat = array('gif','jpg','doc','application/octet-stream');// 文件格式&MIME限定
var $overwrite = ;// 覆盖模式
var $maxSize = ;// 文件最大字节
var $ext;// 文件扩展名
var $thumb = ;// 是否生成缩略图
var $thumbWidth = ;// 缩略图宽
var $thumbHeight = ;// 缩略图高
var $thumbPrefix = "_thumb_";// 缩略图前缀
var $errno;// 错误代号
var $returnArray= array();// 所有文件的返回信息
var $returninfo= array();// 每个文件返回信息 // 构造函数
// @param $savePath 文件保存路径
// @param $fileFormat 文件格式限制数组
// @param $maxSize 文件最大尺寸
// @param $overwriet 是否覆盖 1 允许覆盖 0 禁止覆盖 function Upload($savePath, $fileFormat='',$maxSize = , $overwrite = ) {
$this->setSavepath($savePath);
$this->setFileformat($fileFormat);
$this->setMaxsize($maxSize);
$this->setOverwrite($overwrite);
$this->setThumb($this->thumb, $this->thumbWidth,$this->thumbHeight);
$this->errno = ;
} // 上传
// @param $fileInput 网页Form(表单)中input的名称
// @param $changeName 是否更改文件名
function run($fileInput,$changeName = ){
if(isset($_FILES[$fileInput])){
$fileArr = $_FILES[$fileInput];
if(is_array($fileArr['name'])){//上传同文件域名称多个文件
for($i = ; $i < count($fileArr['name']); $i++){
$ar['tmp_name'] = $fileArr['tmp_name'][$i];
$ar['name'] = $fileArr['name'][$i];
$ar['type'] = $fileArr['type'][$i];
$ar['size'] = $fileArr['size'][$i];
$ar['error'] = $fileArr['error'][$i];
$this->getExt($ar['name']);//取得扩展名,赋给$this->ext,下次循环会更新
$this->setSavename($changeName == ? '' : $ar['name']);//设置保存文件名
if($this->copyfile($ar)){
$this->returnArray[] = $this->returninfo;
}else{
$this->returninfo['error'] = $this->errmsg();
$this->returnArray[] = $this->returninfo;
}
}
return $this->errno ? false : true;
}else{//上传单个文件
$this->getExt($fileArr['name']);//取得扩展名
$this->setSavename($changeName == ? '' : $fileArr['name']);//设置保存文件名
if($this->copyfile($fileArr)){
$this->returnArray[] = $this->returninfo;
}else{
$this->returninfo['error'] = $this->errmsg();
$this->returnArray[] = $this->returninfo;
}
return $this->errno ? false : true;
}
return false;
}else{
$this->errno = ;
return false;
}
} // 单个文件上传
// @param $fileArray 文件信息数组
function copyfile($fileArray){
$this->returninfo = array();
// 返回信息
$this->returninfo['name'] = $fileArray['name'];
$this->returninfo['md5'] = @md5_file($fileArray['tmp_name']);
$this->returninfo['saveName'] = $this->saveName;
$this->returninfo['size'] = number_format( ($fileArray['size'])/ , , '.', ' ');//以KB为单位
$this->returninfo['type'] = $fileArray['type'];
// 检查文件格式
if (!$this->validateFormat()){
$this->errno = ;
return false;
}
// 检查目录是否可写
if(!@is_writable($this->savePath)){
$this->errno = ;
return false;
}
// 如果不允许覆盖,检查文件是否已经存在
//if($this->overwrite == 0 && @file_exists($this->savePath.$fileArray['name'])){
// $this->errno = 13;
// return false;
//}
// 如果有大小限制,检查文件是否超过限制
if ($this->maxSize != ){
if ($fileArray["size"] > $this->maxSize){
$this->errno = ;
return false;
}
}
// 文件上传
if(!@move_uploaded_file($fileArray["tmp_name"], $this->savePath.$this->saveName)){
$this->errno = $fileArray["error"];
return false;
}elseif( $this->thumb ){// 创建缩略图
$CreateFunction = "imagecreatefrom".($this->ext == 'jpg' ? 'jpeg' : $this->ext);
$SaveFunction = "image".($this->ext == 'jpg' ? 'jpeg' : $this->ext);
if (strtolower($CreateFunction) == "imagecreatefromgif"
&& !function_exists("imagecreatefromgif")) {
$this->errno = ;
return false;
} elseif (strtolower($CreateFunction) == "imagecreatefromjpeg"
&& !function_exists("imagecreatefromjpeg")) {
$this->errno = ;
return false;
} elseif (!function_exists($CreateFunction)) {
$this->errno = ;
return false;
} $Original = @$CreateFunction($this->savePath.$this->saveName);
if (!$Original) {$this->errno = ; return false;}
$originalHeight = ImageSY($Original);
$originalWidth = ImageSX($Original);
$this->returninfo['originalHeight'] = $originalHeight;
$this->returninfo['originalWidth'] = $originalWidth;
/*
if (($originalHeight < $this->thumbHeight
&& $originalWidth < $this->thumbWidth)) {
// 如果比期望的缩略图小,那只Copy
move_uploaded_file($this->savePath.$this->saveName,
$this->savePath.$this->thumbPrefix.$this->saveName);
} else {
if( $originalWidth > $this->thumbWidth ){// 宽 > 设定宽度
$thumbWidth = $this->thumbWidth ;
$thumbHeight = $this->thumbWidth * ( $originalHeight / $originalWidth );
if($thumbHeight > $this->thumbHeight){// 高 > 设定高度
$thumbWidth = $this->thumbHeight * ( $thumbWidth / $thumbHeight );
$thumbHeight = $this->thumbHeight ;
}
}elseif( $originalHeight > $this->thumbHeight ){// 高 > 设定高度
$thumbHeight = $this->thumbHeight ;
$thumbWidth = $this->thumbHeight * ( $originalWidth / $originalHeight );
if($thumbWidth > $this->thumbWidth){// 宽 > 设定宽度
$thumbHeight = $this->thumbWidth * ( $thumbHeight / $thumbWidth );
$thumbWidth = $this->thumbWidth ;
}
}
*/
$radio=max(($originalWidth/$this->thumbWidth),($originalHeight/$this->thumbHeight));
$thumbWidth=(int)$originalWidth/$radio;
$thumbHeight=(int)$originalHeight/$radio; if ($thumbWidth == ) $thumbWidth = ;
if ($thumbHeight == ) $thumbHeight = ;
$createdThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
if ( !$createdThumb ) {$this->errno = ; return false;}
if ( !imagecopyresampled($createdThumb, $Original, , , , ,
$thumbWidth, $thumbHeight, $originalWidth, $originalHeight) )
{$this->errno = ; return false;}
if ( !$SaveFunction($createdThumb,
$this->savePath.$this->thumbPrefix.$this->saveName) )
{$this->errno = ; return false;} }
// 删除临时文件
/*
if(!@$this->del($fileArray["tmp_name"])){
return false;
}
*/
return true;
} // 文件格式检查,MIME检测
function validateFormat(){
if(!is_array($this->fileFormat)
|| in_array(strtolower($this->ext), $this->fileFormat)
|| in_array(strtolower($this->returninfo['type']), $this->fileFormat) )
return true;
else
return false;
}
// 获取文件扩展名
// @param $fileName 上传文件的原文件名
function getExt($fileName){
$ext = explode(".", $fileName);
$ext = $ext[count($ext) - ];
$this->ext = strtolower($ext);
} // 设置上传文件的最大字节限制
// @param $maxSize 文件大小(bytes) 0:表示无限制
function setMaxsize($maxSize){
$this->maxSize = $maxSize;
}
// 设置文件格式限定
// @param $fileFormat 文件格式数组
function setFileformat($fileFormat){
if(is_array($fileFormat)){$this->fileFormat = $fileFormat ;}
} // 设置覆盖模式
// @param overwrite 覆盖模式 1:允许覆盖 0:禁止覆盖
function setOverwrite($overwrite){
$this->overwrite = $overwrite;
} // 设置保存路径
// @param $savePath 文件保存路径:以 "/" 结尾,若没有 "/",则补上
function setSavepath($savePath){
$this->savePath = substr( str_replace("\\","/", $savePath) , -) == "/"
? $savePath : $savePath."/";
} // 设置缩略图
// @param $thumb = 1 产生缩略图 $thumbWidth,$thumbHeight 是缩略图的宽和高
function setThumb($thumb, $thumbWidth = ,$thumbHeight = ){
$this->thumb = $thumb;
if($thumbWidth) $this->thumbWidth = $thumbWidth;
if($thumbHeight) $this->thumbHeight = $thumbHeight;
} // 设置文件保存名
// @param $saveName 保存名,如果为空,则系统自动生成一个随机的文件名
function setSavename($saveName){
if ($saveName == ''){ // 如果未设置文件名,则生成一个随机文件名
$name = date('YmdHis')."_".rand(,).'.'.$this->ext;
//判断文件是否存在,不允许重复文件
if(file_exists($this->savePath . $name)){
$name = setSavename($saveName);
}
} else {
$name = $saveName;
}
$this->saveName = $name;
} // 删除文件
// @param $fileName 所要删除的文件名
function del($fileName){
if(!@unlink($fileName)){
$this->errno = ;
return false;
}
return true;
} // 返回上传文件的信息
function getInfo(){
return $this->returnArray;
} // 得到错误信息
function errmsg(){
$uploadClassError = array(
=>'There is no error, the file uploaded with success. ',
=>'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
=>'The uploaded file exceeds the MAX_FILE_SIZE that was specified in the HTML form.',
=>'The uploaded file was only partially uploaded. ',
=>'No file was uploaded. ',
=>'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3. ',
=>'Failed to write file to disk. Introduced in PHP 5.1.0. ',
=>'Input name is not unavailable!',
=>'The uploaded file is Unallowable!',
=>'Directory unwritable!',
=>'File exist already!',
=>'File is too big!',
=>'Delete file unsuccessfully!',
=>'Your version of PHP does not appear to have GIF thumbnailing support.',
=>'Your version of PHP does not appear to have JPEG thumbnailing support.',
=>'Your version of PHP does not appear to have pictures thumbnailing support.',
=>'An error occurred while attempting to copy the source image .
Your version of php ('.phpversion().') may not have this image type support.',
=>'An error occurred while attempting to create a new image.',
=>'An error occurred while copying the source image to the thumbnail image.',
=>'An error occurred while saving the thumbnail image to the filesystem.
Are you sure that PHP has been configured with both read and write access on this folder?',
);
if ($this->errno == )
return false;
else
return $uploadClassError[$this->errno];
}
} ?>
如何使用这个类呢?
<?php
//如果收到表单传来的参数,则进行上传处理,否则显示表单
if(isset($_FILES['uploadinput'])){
//建目录函数,其中参数$directoryName最后没有"/",
//要是有的话,以'/'打散为数组的时候,最后将会出现一个空值
function makeDirectory($directoryName) {
$directoryName = str_replace("\\","/",$directoryName);
$dirNames = explode('/', $directoryName);
$total = count($dirNames) ;
$temp = '';
for($i=; $i<$total; $i++) {
$temp .= $dirNames[$i].'/';
if (!is_dir($temp)) {
$oldmask = umask();
if (!mkdir($temp, )) exit("不能建立目录 $temp");
umask($oldmask);
}
}
return true;
} if($_FILES['uploadinput']['name'] <> ""){
//包含上传文件类
require_once ('upload_class.php');
//设置文件上传目录
$savePath = "upload";
//创建目录
makeDirectory($savePath);
//允许的文件类型
$fileFormat = array('gif','jpg','jpge','png');
//文件大小限制,单位: Byte,1KB = 1000 Byte
//0 表示无限制,但受php.ini中upload_max_filesize设置影响
$maxSize = ;
//覆盖原有文件吗? 0 不允许 1 允许
$overwrite = ;
//初始化上传类
$f = new Upload( $savePath, $fileFormat, $maxSize, $overwrite);
//如果想生成缩略图,则调用成员函数 $f->setThumb();
//参数列表: setThumb($thumb, $thumbWidth = 0,$thumbHeight = 0)
//$thumb=1 表示要生成缩略图,不调用时,其值为 0
//$thumbWidth 缩略图宽,单位是像素(px),留空则使用默认值 130
//$thumbHeight 缩略图高,单位是像素(px),留空则使用默认值 130
$f->setThumb(); //参数中的uploadinput是表单中上传文件输入框input的名字
//后面的0表示不更改文件名,若为1,则由系统生成随机文件名
if (!$f->run('uploadinput',)){
//通过$f->errmsg()只能得到最后一个出错的信息,
//详细的信息在$f->getInfo()中可以得到。
echo $f->errmsg()."<br>\n";
}
//上传结果保存在数组returnArray中。
echo "<pre>";
print_r($f->getInfo());
echo "</pre>";
}
}else{
?>
<form enctype="multipart/form-data" action="" method="POST">
Send this file: <br />
<input name="uploadinput[]" type="file"><br />
<input name="uploadinput[]" type="file"><br />
<input name="uploadinput[]" type="file"><br />
<input type="submit" value="Send File"><br />
</form>
<?php
} ?>

PHP多文件上传类的更多相关文章

  1. ASP.NET 文件上传类 简单好用

    调用: UploadFile uf = new UploadFile(); /*可选参数*/ uf.SetIsUseOldFileName(true);//是否使用原始文件名作为新文件的文件名(默认: ...

  2. PHP 文件上传类

    FileUpload.;                $];                $_newname = date(,). :                             To ...

  3. php 文件上传类 实例分享

    最近在研究php上传的内容,找到一个不错的php上传类,分享下. <?php /** * 文件上传类 * class: uploadFile * edit: www.jbxue.com */ c ...

  4. [上传下载] C#FileUp文件上传类 (转载)

    点击下载 FileUp.zip 主要功能如下 .把上传的文件转换为字节数组 .流转化为字节数组 .上传文件根据FileUpload控件上传 .把Byte流上传到指定目录并保存为文件 看下面代码吧 // ...

  5. ThinkPHP文件上传类

    TP框架自带文件上传类使用: 类文件在ThinkPHP/Library/Think/默认在目录下 public function upload(){ $upload = new \Think\Uplo ...

  6. C#文件上传类,文件流,字节数组等

    using System;using System.IO;using System.Web;using System.Web.UI.WebControls; namespace DotNet.Util ...

  7. Php文件上传类class.upload.php

    简介 Class.upload.php是用于管理上传文件的php文件上传类, 它可以帮助你快速的给自己的网站集成上传文件功能.不仅如此,此分类还有一些列的处理功能,可以对上传的文件或者本地的文件进行处 ...

  8. 自定义MVC框架之工具类-文件上传类

    截止目前已经改造了3个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 该文件上传类功能如下: 1,允许定制上传的文件类型,文件mime信息,文 ...

  9. php 文件上传类,功能相当齐全,留作开发中备用吧。

    收藏一个经典好用的php 文件上传类,功能相当齐全,留作开发中备用吧. 好东西,大家都喜欢,才是真的好,哈哈!!! <?php  /**   * 文件上传类   */  class upload ...

  10. PHP实现的多文件上传类及用法示例

    这篇文章主要介绍了PHP实现的多文件上传类及用法,详细分析了php实现的多文件上传类与具体的使用技巧,需要的朋友可以参考下 1.upFiles.css.php 文件 <?php class Up ...

随机推荐

  1. Why The Golden Age Of Machine Learning is Just Beginning

    Why The Golden Age Of Machine Learning is Just Beginning Even though the buzz around neural networks ...

  2. 繁华模拟赛 Vicent与游戏

    #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #i ...

  3. Java中的封装

    在前面的一些日子里,一只都在学习C#语言,使用C#在做一些小项目的,今天转到了Java的学习,还是感觉有点的不习惯,没有以前的中文界面的,全是英文.写起代码来都一直保持着C#中的编码的习惯,但是学习J ...

  4. Linux简单的常用命令——纯手打(慢慢积累)

    ==============linux下快捷键==================ctrl+insert 复制shift +insert 粘贴 输入文件名的前三个字母,按tab键自动补全文件名 在vi ...

  5. Windows性能计数器

    LogicalDisk\% Free Space 它测量选定逻辑磁盘上的可用空间百分比.请注意,如果此值低于 15%,则表示可用空间不足,操作系统无法存储关键文件.一个最直接的解决方案是增加更多的磁盘 ...

  6. hiho #1272 买零食 [Offer收割]编程练习赛2

    #1272 : 买零食 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho很喜欢在课间去小卖部买零食.然而不幸的是,这个学期他又有在一教的课,而一教的小卖部姐姐以冷若冰 ...

  7. Linux 查看网络连接状态

    CLOSED:无连接是活动的或正在进行ESTABLISED:已建立连线的状态:SYN_SENT:发出主动连线 (SYN 标志) 的连线封包:SYN_RECV:接收到一个要求连线的主动连线封包:FIN_ ...

  8. Android程序启动加载动画实现

    package com.example.bmob_test.ui;//程序启动动画,图片颜色由浅到深,方法一 import com.example.bmob_test.LogActivity; imp ...

  9. 一个很不错的适合PHPER们书单,推荐给大家【转】

    来我博客的访客们中,有一些是PHP的初学者,是不是很迷茫PHP应该怎么学?应该买什么样的书?到处问人,到处求助?这下好了. 正好看到黑夜路人在博客上推荐了一个书单,看上去都非常不错,很多我也没有读过, ...

  10. Firefox上Web开发工具库一览

    Firefox的目标之一就是尽可能地使web开发者的生活更简单高效,并通过提供工具和具有很强扩展性的浏览器使人们创造出神奇的东西.使web开发者使用Firefox的时候,浏览器可以提供大量开发工具和选 ...