php 多文件上传经典代码

<?php
require_once("include/upload.class.php");
if($_POST["button"])
{
        //print_r($_FILES);
        //多个上传
        //$upload = new TTRUpload($_FILES,"ANY");//同下

        $upload = new TTRUpload(array($_FILES["file1"],$_FILES["file2"],$_FILES["file3"],$_FILES["file4"]),"ANY");

        //单个上传
        //$upload = new TTRUpload($_FILES["file1"]);
        $upload->upload();
        echo $upload->getUploadFileName();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <input type="file" name="file1" id="file1" />
  <br />
  <input type="file" name="file2" id="file2" />
  <br />
  <input type="file" name="file3" id="file3" />
  <br />
  <input type="file" name="file4" id="file4" />
  <br />
  <input type="submit" name="button" id="button" value="Submit" />
</form>
</body>
</html>

<?php
class TTRUpload extends Error
{
        const filesize=81200000;
        private $uploadpath="uploadfile/";
        private $savepath=null;
        private $uploadfilename=null;                                //单个文件为文件名,批量文件为xxxx|xxxx格式,请注意
        private $ext=array("jpg","gif","png");
        private $error=null;
        private $file=null;       
        private $uploadtype=null;
        private $filename=null;
       
        //构造函数,$type:ONE单个上传 ANY批量上传;
        public function __construct($file,$type="ONE")
        {
                if($type!="ONE" && $type!="ANY")
                {
                        echo "<script language='javascript'>alert('初始化请选择ONE或者ANY')</script>";
                        exit;
                }
                $this->uploadtype=$type;
                $this->file=$file;
        }
       
        private function createFileName()
        {
                return $this->filename="TTR_".time().$this->getRandomN(4);
        }
               
        private function getUploadPath()
        {
                if(substr($this->uploadpath,-1,1)!="/")
                {
                        $this->savepath=$this->uploadpath."/".date("Ym");
                }else{
                        $this->savepath=$this->uploadpath.date("Ym");
                }
                $this->savepath=$this->getFolder($this->savepath);
                return true;
        }
       
        private function getFileExt($tempfilename)
        {
                return end(explode(".",$tempfilename));
        }
       
        private function getExt()
        {
                if(in_array(strtolower($this->getFileExt($tempfilename)),$this->ext))
                {
                        return true;
                }else{
                        return false;       
                }
        }
       
        private function getFolder($folder)
        {
                if(!is_dir($folder))
                {
                        mkdir($folder);
                }
                return $folder."/";
        }
       
       
        public function upload()
        {
                if($this->uploadtype=="ONE")
                {
                       

                        if($this->getExt($this->file["type"]))
                        {
                               
                                parent::errorExt();
                               
                        }else if($this->file["size"]>self::filesize){
                               
                                parent::errorFileSize();
                               
                        }else if(!$this->getUploadPath()){
                               
                                parent::errorUploadPath();
                               
                        }else{
                                $filenametemp=$this->createFileName();
                                $filename=$this->savepath.$filenametemp.".".$this->getFileExt($this->file["name"]);
                                if(move_uploaded_file($this->file["tmp_name"],$filename))
                                {       
                                        $this->uploadfilename=$filenametemp;
                                        parent::okMoved();                       
                                       
                               
                                }else{
                                        parent::errorMoveUpload();
                                }
                        }
                }else if($this->uploadtype=="ANY"){

                        for($i=0;$i<count($this->file);$i++)
                        {
                       
                                if($this->getExt($this->file[$i]["type"]))
                                {
                                        parent::errorExt();
                                       
                                }else if($this->file[$i]["size"]>self::filesize){
                                       
                                        parent::errorFileSize();
                                       
                                }else if(!$this->getUploadPath()){
                                       
                                        parent::errorUploadPath();
                                       
                                }else{
                                        $filenametemp=$this->createFileName();
                                        $filename=$this->savepath.$filenametemp.".".$this->getFileExt($this->file[$i]["name"]);
                                        if(move_uploaded_file($this->file[$i]["tmp_name"],$filename))
                                        {       
                                                $str.=$filenametemp."|";
                                               
                                        }else{
                                                parent::errorMoveUpload();
                                        }
                                       
                                }
                               
                        }
                        $this->uploadfilename=substr($str,0,strlen($str)-1);       
                        parent::okMoved();
                }
        }
       
        public function getUploadFileName()
        {
                return $this->uploadfilename;
        }
       
        public function setUploadPath($path)
        {
                $this->uploadpath=$path;
        }
       
       
        private function getRandomN($n)
        {
                if ($n < 1 || $n>10)  return "";
       
                $ary_num= array(0,1,2,3,4,5,6,7,8,9);
                $return ="";
                for ($i=0;$i<$n;$i++)
                {
                        $randn = rand(0,9-$i);
                        $return .= $ary_num[$randn];
                        $ary_num[$randn] = $ary_num[9-$i];
                }
                return $return;
        }

       
       
        public function __destruct()
        {
                $this->uploadfilename=null;
                $this->uploadtype=null;
                $this->file=null;
                $this->savepath=null;
        }
       
}

class Error
{
        public static function errorFileSize()
        {
                echo "超出最大上传限制";
        }
       
        public static function errorExt()
        {
                echo "此类文件不允许上传";
        }
       
        public static function errorUploadPath()
        {
                echo "上传路径不正确";
        }
       
        public static function errorMoveUpload()
        {
                echo "上传失败";
        }
       
        public static function okMoved()
        {
                echo "上传成功!";
        }
       
        public static function okArrayMoved()
        {
                echo "上传成功!";
        }
}

时间: 2024-09-30 05:26:37

php 多文件上传经典代码的相关文章

php文件上传经典代码

php文件上传经典代码 function FileUpload( $resourceType, $currentFolder, $sCommand ) {  if (!isset($_FILES)) {   global $_FILES;  }  $sErrorNumber = '0' ;  $sFileName = '' ;  if ( isset( $_FILES['NewFile'] ) && !is_null( $_FILES['NewFile']['tmp_name'] ) )

PHP中codeigniter文件上传类代码实例

  codeigniter文件上传类代码实例 文件上传类 CodeIgniter 的文件上传类允许文件被上传.您可以设置指定上传某类型的文件及指定大小的文件. 处理过程 上传文件普遍的过程: 一个上传文件用的表单,允许用户选择一个文件并上传它. 当这个表单被提交,该文件被上传到指定的目录. 同时,该文件将被验证是否符合您设定的要求. 一旦文件上传成功,还要返回一个上传成功的确认窗口. 这里有一个简短的教程来显示这个过程.此后你将会找到相关的参考信息. 创建上传表单 运用文本编辑器创建一个名为up

php多文件上传实现代码

 这篇文章主要介绍了php多文件上传实现代码,需要的朋友可以参考下 index_uploads.php    代码如下: <html> <head>     <meta charset="utf-8">     <title>index_uploads</title> </head> <body>     <form action="uploads.php" method=&q

Spring学习笔记2之表单数据验证、文件上传实例代码_java

在上篇文章给大家介绍了Spring学习笔记1之IOC详解尽量使用注解以及java代码,接下来本文重点给大家介绍Spring学习笔记2之表单数据验证.文件上传实例代码,具体内容,请参考本文吧! 一.表单数据验证 用户注册时,需要填写账号.密码.邮箱以及手机号,均为必填项,并且需要符合一定的格式.比如账号需要32位以内,邮箱必须符合邮箱格式,手机号必须为11位号码等.可以采用在注册时验证信息,或者专门写一个工具类用来验证:来看下在SpringMVC中如何通过简单的注释实现表单数据验证. 在javax

关于文件上传的代码。 可是点上传按钮一点反应没有啊。求指明错误

问题描述 <scriptlanguage="javascript"type="text/javascript">//<!CDATA[functionbtnUpLoad_onclick(){if((fileUpLoad.PostedFile.FileName!=null)&&(fileUpLoad.PostedFile.FilName!="")){StringstrFilePath=fileUpLoad.Poste

一个实用的php 文件上传类代码

这里是来自网络朋友的一个实现的文件上传类代码,我们详细的介绍了每个变量的用处,下面看看吧,有需要可以参考一下. <?php教程  /**   * 文件上传类   */  class uploadFile {   public $max_size = '1000000';//设置上传文件大小   public $file_name = 'date';//重命名方式代表以时间命名,其他则使用给予的名称   public $allow_types;//允许上传的文件扩展名,不同文件类型用"|&q

php Ajax实现异步文件上传的代码

php教程 ajax实现异步文件上传的代码 1:取得file对象 2:读取2进制数据 3:模拟http请求,把数据发送出去(这里通常比较麻烦) 在forefox下使用 xmlhttprequest 对象的 sendasbinary 方法发送数据: 4:完美实现 遇到的问题 目前仅有 firefox 可以正确上传文件.(chrome也可以采google.gears上传) 对于从firefox和chrome下读取到的文件数据好像不一样(不知道是否是调试工具的原因) chrome以及其他高级浏览器没有

php图片文件上传实例代码

 代码如下 复制代码 <html xmlns="http://www.111cn.net/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=gb2312" /> <title>php教程图片文件上传实例代码</title> </head> <body> &

多文件上传类代码

    真正支持单文件和多文件上传类代码,修正了$_FILES[$field]['name']中的$field不能用变量只能和表单中的文件名name="userfile"一致的缺点$_FILES['userfile']['name'],这里<input type="file" name="userfile"> 中的文件名可以随意取. //index.htm1.单文件上传<form method="post"