搜藏一个php文件上传类

<?php
/**
 * 上传文件类
 * @param _path : 服务器文件存放路径
 * @param _allowType : 允许上传的文件类型和所对应的MIME
 * @param _file : 上传的文件信息
 */
class Upload{

    private $_path;
    private $_allowType;
    private $_file;
    /**
     * 构造函数
     * @param string : 服务器上存放上传文件的路径
     */
    function __construct( $path = ‘‘ )
    {
        $this->_path = $path;
        $this->_allowType = array(
                // images
                ‘bmp‘ => ‘image/x-ms-bmp‘,
                ‘jpg‘ => ‘image/jpeg‘,
                ‘jpeg‘ => ‘image/jpeg‘,
                ‘gif‘ => ‘image/gif‘,
                ‘png‘ => ‘image/png‘,
                ‘tif‘ => ‘image/tiff‘,
                ‘tiff‘ => ‘image/tiff‘,
                ‘tga‘ => ‘image/x-targa‘,
                ‘psd‘ => ‘image/vnd.adobe.photoshop‘,
                //文本
                ‘txt‘ => ‘text/plain‘,
                ‘php‘ => ‘text/x-php‘,
                ‘html‘ => ‘text/html‘,
                ‘htm‘ => ‘text/html‘,
                ‘js‘ => ‘text/javascript‘,
                ‘css‘ => ‘text/css‘,
                ‘rtf‘ => ‘text/rtf‘,
                ‘rtfd‘ => ‘text/rtfd‘,
                ‘py‘ => ‘text/x-python‘,
                ‘java‘ => ‘text/x-java-source‘,
                ‘rb‘ => ‘text/x-ruby‘,
                ‘sh‘ => ‘text/x-shellscript‘,
                ‘pl‘ => ‘text/x-perl‘,
                ‘sql‘ => ‘text/x-sql‘,
                //应用
                ‘exe‘ => ‘application/octet-stream‘,
                ‘doc‘ => ‘application/vnd.ms-word‘,
                ‘docx‘ => ‘application/vnd.ms-word‘,
                ‘xls‘ => ‘application/vnd.ms-excel‘,
                ‘ppt‘ => ‘application/vnd.ms-powerpoint‘,
                ‘pps‘ => ‘application/vnd.ms-powerpoint‘,
                ‘pdf‘ => ‘application/pdf‘,
                ‘xml‘ => ‘application/xml‘,
                //音频
                ‘mp3‘ => ‘audio/mpeg‘,
                ‘mid‘ => ‘audio/midi‘,
                ‘ogg‘ => ‘audio/ogg‘,
                ‘mp4a‘ => ‘audio/mp4‘,
                ‘wav‘ => ‘audio/wav‘,
                ‘wma‘ => ‘audio/x-ms-wma‘,
                //视频
                ‘avi‘ => ‘video/x-msvideo‘,
                ‘dv‘ => ‘video/x-dv‘,
                ‘mp4‘ => ‘video/mp4‘,
                ‘mpeg‘ => ‘video/mpeg‘,
                ‘mpg‘ => ‘video/mpeg‘,
                ‘mov‘ => ‘video/quicktime‘,
                ‘wm‘ => ‘video/x-ms-wmv‘,
                ‘flv‘ => ‘video/x-flv‘,
                ‘mkv‘ => ‘video/x-matroska‘
            );
    }
    /**
     * 上传函数
     * @param  string : 表单元素的name 值
     * @return [type]
     */
    public function upload( $txtName = ‘‘ )
    {
        $this->_file = $_FILES[$txtName];
        if( $this->_file[‘error‘] == 0){
            $fileType = end( explode(‘.‘, $this->_file[‘name‘] ));
            $allowType = array();
            foreach( $this->_allowType as $item=>$value ){
                $allowType[] = $item;
            }
            if( !in_array($fileType, $allowType)){
                die(‘上传的文件格式不正确!‘);
            }else{
                if(move_uploaded_file($this->file[‘tmp_name‘], ($this->path).$this->file[‘name‘]))
                    {
                        echo "<script>alert(‘上传成功!‘)</script>";
                    }
                else
                    {
                        echo "<script>alert(‘上传失败!‘);</script>";
                    }
            }

        }else{
            //没有正确上传
            switch ($this->file[‘error‘]){
                case 1:
                    die(‘文件大小超过系统限制。‘);
                    break;
                case 2:
                    die(‘文件大小超过预定义限制。‘);
                    break;
                case 3:
                    die(‘文件为完全上传。‘);
                    break;
                case 4:
                    die(‘未上传任何文件。‘);
                    break;
                default:
                    die(‘上传出错‘);
                    break;
            }
        }
    }
    //end upload
}
时间: 2024-11-04 14:32:48

搜藏一个php文件上传类的相关文章

分享一个PHP文件上传类

该类用于处理文件上传,可以上传一个文件,也可以上传多个文件. 包括的成员属性有: 1 private $path = "./uploads"; //上传文件保存的路径 2 private $type = array('jpg','gif','png'); //设置限制上传文件的类型 3 private $maxSize = 1000000; //限制文件上传大小(字节) 4 private $isRandName = true; //设置是否随机重命名文件, false不随机 5 pr

ajax结合文件上传类进行多文件的单个上传

今天做项目的时候碰见一个问题:之前一个同事离职之前做了一个网站,有一个上传商品详细图片的功能,当时已经完成,但是由于后期程序的有更改以及更改的程序员的水平也是参差不齐,最后导致程序bug很多,由于当时用的是一个框架,最终也没找到说明文档,后来我就重新写了一个结合ajax上传文件的upload.classs.php虽然界面欠缺美观,但是通俗易懂好维护. //首先是页面. index.php <!DOCTYPE html> <html lang="en"> <

面向对象中的文件上传类

<?php /** *文件上传类 * **/ class Upload { //上传到哪个目录 protected $path = './upload/'; //准许的MIME protected $allowmime = ['image/png','image/jpg','image/jpeg','image/pjpeg','image/bmp','image/wbmp','image/gif','image/x-png']; //准许的后缀 protected $allowsubfix = 

PHP文件上传类(页面和调用部分)

<!--upform.html内容--> <form action="upload.php" method="post" enctype="multipart/form-data" > name: <input type="text" name="username" value="" /><br> <input type="

文件上传类

<?php /** file: fileupload.class.php 文件上传类FileUpload 本类的实例对象用于处理上传文件,可以上传一个文件,也可同时处理多个文件上传 */ class FileUpload { private $path = "./uploads"; //上传文件保存的路径 private $allowtype = array('jpg','gif','png'); //设置限制上传文件的类型 private $maxsize = 1000000;

PHP文件上传类

<form action="upload.php" method="post" enctype="multipart/form-data" > name: <input type="text" name="username" value="" /><br> <input type="hidden" name="MAX

php 文件上传类

<?php /** file: fileupload.class.php 文件上传类FileUpload 本类的实例对象用于处理上传文件,可以上传一个文件,也可同时处理多个文件上传 */ class FileUpload { private $path = "./uploads"; //上传文件保存的路径 private $allowtype = array('jpg','gif','png'); //设置限制上传文件的类型 private $maxsize = 1000000;

PHP基础------文件上传类

<?php //文件上传类 $upImage = $_FILES["img"]; //Array //( // [name] => gou.jpg // [type] => image/jpeg // [tmp_name] => D:\wamp\tmp\php311F.tmp // [error] => 0 // [size] => 9488 //) class upLoad{ private $_arr; //构造函数,把图像信息赋值给$_arr字

文件上传类,实现文件上传功能

/** *==================================================================  * upload.class.php 文件上传类,实现文件上传功能 * 2013年3月27日0:37:15 *================================================================== */ class Upload{    private $path;   //文件上传目录    privat