Home >Backend Development >PHP Tutorial >PHP file upload class_PHP tutorial

PHP file upload class_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:06:09955browse

php file upload class and php file upload and php upload pictures and other php upload codes are just different in form.

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) )
$this-> _origin_paths[$file] = $origin_path;
}
function upload()
{
foreach( $this->_origin_paths as $file => $origin_path )
{
             $result = $this->_uploadFile($file, $origin_path);

                                                                                                                                                                                                                                                                                          through 🎜> * @desc Upload attachment
* @return Success returns Success, failure returns failure type
* @param $file file name $orgin_path file path
*/
function _uploadFile($file, $origin_path) //Upload attachment
{
$ffile = $_FILES[$file]['tmp_name']; //The temporary file name stored on the server after the file is uploaded.
$ FNAME = $ _Files [$ File] ['name']; // The original name of the client machine file.
$fsize = $_FILES[$file]['size']; //The size of the uploaded file
$ftype = $_FILES[$file]['type']; //The MIME type of the file
                                                                                                                      ter) && ! is_null($this->_ftype_filter) )
                                                                                                                                              foreach($extensions as $extension)
{
if( strtolower(strrchr($fname,'.')) == '.'.strtolower(trim($extension)) )
                      $match = true ;
break;
}
}
if( !$match )
                 return 'ErrorFileTypeFilterNotMatch';
                                                                                                                        –>_rel_dir; ffile, $ fpath . $fname ) or die( 'upload failed!' );
                                                                                                                                      $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<$len; $i++ )
        {
            $num = rand(0, 35);
            if( $num < 10 )
                $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...
Statement:
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