php 文件上传类_PHP教程

WBOY
Freigeben: 2016-07-20 11:06:09
Original
924 Leute haben es durchsucht

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,'.'));
    }
}
?>


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445069.htmlTechArticlephp 文件上传类 和 php 上传文件 以及php 上传图片等php 上传代码只是表现形式不一样了。 ?php class Uploader { var $_base_dir = null; var $_rel_dir = nul...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage