123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- class Uploader
- {
- private $fileField;
- private $file;
- private $config;
- private $oriName;
- private $fileName;
- private $fullName;
- private $fileSize;
- private $fileType;
- private $stateInfo;
- private $stateMap = array(
- "SUCCESS" ,
- "文件大小超出 upload_max_filesize 限制" ,
- "文件大小超出 MAX_FILE_SIZE 限制" ,
- "文件未被完整上传" ,
- "没有文件被上传" ,
- "上传文件为空" ,
- "POST" => "文件大小超出 post_max_size 限制" ,
- "SIZE" => "文件大小超出网站限制" ,
- "TYPE" => "不允许的文件类型" ,
- "DIR" => "目录创建失败" ,
- "IO" => "输入输出错误" ,
- "UNKNOWN" => "未知错误" ,
- "MOVE" => "文件保存时出错",
- "DIR_ERROR" => "创建目录失败"
- );
-
- public function __construct( $fileField , $config , $base64 = false )
- {
- $this->fileField = $fileField;
- $this->config = $config;
- $this->stateInfo = $this->stateMap[ 0 ];
- $this->upFile( $base64 );
- }
-
- private function upFile( $base64 )
- {
-
- if ( "base64" == $base64 ) {
- $content = $_POST[ $this->fileField ];
- $this->base64ToImage( $content );
- return;
- }
-
- $file = $this->file = $_FILES[ $this->fileField ];
- if ( !$file ) {
- $this->stateInfo = $this->getStateInfo( 'POST' );
- return;
- }
- if ( $this->file[ 'error' ] ) {
- $this->stateInfo = $this->getStateInfo( $file[ 'error' ] );
- return;
- }
- if ( !is_uploaded_file( $file[ 'tmp_name' ] ) ) {
- $this->stateInfo = $this->getStateInfo( "UNKNOWN" );
- return;
- }
- $this->oriName = $file[ 'name' ];
- $this->fileSize = $file[ 'size' ];
- $this->fileType = $this->getFileExt();
- if ( !$this->checkSize() ) {
- $this->stateInfo = $this->getStateInfo( "SIZE" );
- return;
- }
- if ( !$this->checkType() ) {
- $this->stateInfo = $this->getStateInfo( "TYPE" );
- return;
- }
- $folder = $this->getFolder();
- if ( $folder === false ) {
- $this->stateInfo = $this->getStateInfo( "DIR_ERROR" );
- return;
- }
- $this->fullName = $folder . '/' . $this->getName();
- if ( $this->stateInfo == $this->stateMap[ 0 ] ) {
- if ( !move_uploaded_file( $file[ "tmp_name" ] , $this->fullName ) ) {
- $this->stateInfo = $this->getStateInfo( "MOVE" );
- }
- }
- }
-
- private function base64ToImage( $base64Data )
- {
- $img = base64_decode( $base64Data );
- $this->fileName = time() . rand( 1 , 10000 ) . ".png";
- $this->fullName = $this->getFolder() . '/' . $this->fileName;
- if ( !file_put_contents( $this->fullName , $img ) ) {
- $this->stateInfo = $this->getStateInfo( "IO" );
- return;
- }
- $this->oriName = "";
- $this->fileSize = strlen( $img );
- $this->fileType = ".png";
- }
-
- public function getFileInfo()
- {
- return array(
- "originalName" => $this->oriName ,
- "name" => $this->fileName ,
- "url" => $this->fullName ,
- "size" => $this->fileSize ,
- "type" => $this->fileType ,
- "state" => $this->stateInfo
- );
- }
-
- private function getStateInfo( $errCode )
- {
- return !$this->stateMap[ $errCode ] ? $this->stateMap[ "UNKNOWN" ] : $this->stateMap[ $errCode ];
- }
-
- private function getName()
- {
- return $this->fileName = time() . rand( 1 , 10000 ) . $this->getFileExt();
- }
-
- private function checkType()
- {
- return in_array( $this->getFileExt() , $this->config[ "allowFiles" ] );
- }
-
- private function checkSize()
- {
- return $this->fileSize <= ( $this->config[ "maxSize" ] * 1024 );
- }
-
- private function getFileExt()
- {
- return strtolower( strrchr( $this->file[ "name" ] , '.' ) );
- }
-
- private function getFolder()
- {
- $pathStr = $this->config[ "savePath" ];
- if ( strrchr( $pathStr , "/" ) != "/" ) {
- $pathStr .= "/";
- }
- $pathStr .= date( "Ymd" );
- if ( !file_exists( $pathStr ) ) {
- if ( !mkdir( $pathStr , 0777 , true ) ) {
- return false;
- }
- }
- return $pathStr;
- }
- }
|