Home > php教程 > php手册 > body text

php 文件上传类

WBOY
Release: 2016-06-13 11:22:07
Original
964 people have browsed it

php 文件上传类php 上传文件以及php 上传图片等php 上传代码只是表现形式不一样了。

class Uploader
{
    var $_base_dir = null;
    var $_rel_dir = null;
    var $_random_fname = false;
    var $_random_fname_len = 5;
    var $_fname_filter = null;
    var $_ftype_filter = null;
    var $_origin_paths = array();

    function Uploader( $base_dir, $rel_dir )
    {
        $this->_base_dir = $base_dir;
        $this->_rel_dir = $rel_dir;
    }
    function setRandomFileName($random_fname, $random_fname_len=5)
    {
        $this->_random_fname = $random_fname;
        $this->_random_fname_len = $random_fname_len;
    }
    function setFileTypeFilter($filter)
    {
        $this->_ftype_filter = $filter;
    }
    function addFile($file, $origin_path='')
    {
        $file = trim($file);
        $origin_path = trim($origin_path);
        if( array_key_exists($file, $this->_origin_paths) )
            return;
        $this->_origin_paths[$file] = $origin_path;
    }
    function upload()
    {
        foreach( $this->_origin_paths as $file => $origin_path )
        {
            $result = $this->_uploadFile($file, $origin_path);

            if( $result != 'Success' )
                return $result;
        }
        return 'Success';
    }
     /*
      * @desc   上传附件
      * @return  成功返回Success 失败返回失败类型
      * @param   $file 文件名 $orgin_path 文件路径
      */
    function _uploadFile($file, $origin_path)  //上传附件
    {
        $ffile = $_FILES[$file]['tmp_name'];     //文件被上传后在服务端储存的临时文件名。
        $fname = $_FILES[$file]['name'];         //客户端机器文件的原名称。
        $fsize = $_FILES[$file]['size'];         //已上传文件的大小
        $ftype = $_FILES[$file]['type'];         //文件的 MIME 类型
        $new_path = '';
       
        if( !empty($fname) && is_uploaded_file($ffile) )
        {
            if( !empty($this->_ftype_filter) && !is_null($this->_ftype_filter) )
            {
                $match = false;
                $extensions = explode(',', $this->_ftype_filter);
                foreach($extensions as $extension)
                {
                    if( strtolower(strrchr($fname,'.')) == '.'.strtolower(trim($extension)) )
                    {
                        $match  = true;
                        break;
                    }
                }
                if( !$match )
                    return 'ErrorFileTypeFilterNotMatch';
            }
            $fpath = $this->_base_dir . $this->_rel_dir;
            if( $this->_random_fname )
             $fname = $this->_getUniqueFileName($fname, $this->_random_fname_len);
            copy( $ffile, $fpath . $fname ) or die( 'upload failed!' );
            $new_path = $this->_rel_dir . $fname;
        }
        if( !empty($origin_path) && !empty($new_path) && $origin_path!=$new_path )
        {
            $this->delete($origin_path);
        }
        if( !empty($new_path) )
            $this->_origin_paths[$file] = $new_path;            
        $Erroe=$_FILES[$file]['error'];   
        switch($Erroe){
          case 1:
              return 'ErrExceedUploadMaxFileSize';
              break;
          case 2:
              return 'ErrExceedHtmlMaxFileSize';
              break;
          case 3:
              return 'ErrPartFileTrans';
              break;
//          case 4:
//              return 'ErrNoFileTrans';
//              break;
          default:
             return 'Success';
        }     
    }
     /*
      * @desc   取得路径 
      * @return  路径
      * @param   无
      */
    function getFilePath()
    {
        return $this->_origin_paths;
    }
    function getFileAbsPath()
    {
        $paths = array();
        foreach( $this->_origin_paths as $path )
        {
            $paths[] = $this->_base_dir . $path;
        }
        return $paths;
    }
    function delete( $fpath )
    {
        if( !empty($fpath) && is_file($this->_base_dir . $fpath) )
            unlink( $this->_base_dir . $fpath ) or die( 'unlink error' );
    }
    function _getUniqueFileName( $fname, $len )
    {
        $timestamp = date('YmdHis');
        srand((double)microtime()*1000000);
        for( $i=0, $randfname=''; $i        {
            $num = rand(0, 35);
            if( $num                 $randfname .=  chr( ord('0')+$num );
            else
                $randfname .=  chr( ord('a')+$num-10 );
        }
        return $timestamp.'_'.$randfname.strtolower(strrchr($fname,'.'));
    }
}
?>


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template