时时商务社区

标题: PHP实现原生态图片上传封装类方法 [打印本页]

作者: qz234    时间: 2018-2-14 05:31

            PHP图片上传类,经典方式,不过上传效率还算可以,我自己用过的一个类,当时对这个类做了些修改,以满足自己特定功能的需要,对PHP熟悉的,可对这个上传类做优化和修改,后附有调用方法,让PHP开发者上传图片轻松容易就做到,先上类代码:
GetFileAttri() )
{
  $pass = false;
}
if( ! $this -> CheckFileMIMEType() )
{
$pass = false;
$this -> error .= die("");
}
if( ! $this -> CheckFileAttri_size() )
{
  $pass = false;
  $this -> error .= die("");
  return false;
}
if ( ! $this -> MoveFileToNewPath() )
{
  $pass = false;
  $this -> error .= die("");
}
return $pass;
}
function GetFileAttri()
{
foreach( $_FILES as $tmp )
{
  $this -> uploadFile = $tmp;
}
return (empty( $this -> uploadFile[ 'name' ])) ? false : true;
}
function CheckFileAttri_size()
{
if ( ! empty ( $this -> fileSize ))
{
  if ( is_numeric( $this -> fileSize ))
  {
  if ($this -> fileSize > 0)
  {
   return ($this -> uploadFile[ 'size' ] > $this -> fileSize * 1024) ? false : true ;
  }
  }
  else
  {
  return false;
  }
}
else
{
  return false;
}
}
function ChangeFileName ($prefix = NULL , $mode)
{// string $prefix , int $mode
$fullName = (isset($prefix)) ? $prefix."_" : NULL ;
switch ($mode)
{
  case 0  : $fullName .= rand( 0 , 100 ). "_" .strtolower(date ("ldSfFYhisa")) ; break;
  case 1  : $fullName .= rand( 0 , 100 ). "_" .time(); break;
  case 2  : $fullName .= rand( 0 , 10000 ) . time();  break;
  default : $fullName .= rand( 0 , 10000 ) . time();  break;
}
return $fullName;
}
function MoveFileToNewPath()
{
$newFileName = NULL;
$newFileName = $this -> ChangeFileName( $this -> filePrefix , 2 ). "." . $this -> GetFileTypeToString();
//检查目录是否存在,不存在则创建,当时我用的时候添加了这个功能,觉得没用的就注释掉吧
/*
$isFile = file_exists( $this -> accessPath);
clearstatcache();
  if( ! $isFile && !is_dir($this -> accessPath) )
  {
    echo $this -> accessPath;
  @mkdir($this -> accessPath);
  }*/
$array_dir=explode("/",$this -> accessPath);//把多级目录分别放到数组中
for($i=0;$i uploadFile[ 'tmp_name' ] , realpath( $this -> accessPath ) . "/" .$newFileName ) )
  {
    $this -> newFileName = $newFileName;
      return true;
  }else{
    return false;
  }
/////////////////////////////////////////////////////////////////////////////////////////////////
}
function CheckFileExist( $path = NULL)
{
return ($path == NULL) ? false : ((file_exists($path)) ? true : false);
}
function GetFileMIME()
{
return $this->GetFileTypeToString();
}
function CheckFileMIMEType()
{
$pass = false;
$defineTypeList = strtolower( $this ->defineTypeList);
$MIME = strtolower( $this -> GetFileMIME());
if (!empty ($defineTypeList))
{
  if (!empty ($MIME))
  {
  foreach(explode("|",$defineTypeList) as $tmp)
  {
   if ($tmp == $MIME)
   {
   $pass = true;
   }
  }
  }
  else
  {
  return false;
  }   
  }
  else
  {
  return false;
  }
  return $pass;
}
function GetFileTypeToString()
{
if( ! empty( $this -> uploadFile[ 'name' ] ) )
{
  return substr( strtolower( $this -> uploadFile[ 'name' ] ) , strlen( $this -> uploadFile[ 'name' ] ) - 3 , 3 );
}
}
}
?>
以下是PHP上传类的调用方法,PHP代码如下:
  accessPath ='upload';//图片上传的目录,这里是当前目录下的upload目录,可自己修改
    if ( $tmp -> TODO() )
    {
      $filename=$tmp -> newFileName;//生成的文件名
      echo "图片上传成功,路径为:upload/".$filename;
    }else{
      echo $tmp -> error;
    }     
}
else{
  echo "没有图片数据可上传";
}
?>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
            
            
您可能感兴趣的文章:
  • PHP图片上传类带图片显示
  • php 图片上传类代码
  • php 图片加水印与上传图片加水印php类
  • 超级好用的一个php上传图片类(随机名,缩略图,加水印)
  • php另类上传图片的方法(PHP用Socket上传图片)
  • PHP实现多图片上传类实例
  • php上传图片类及用法示例
  • php图片上传类 附调用方法
  • PHP上传图片类显示缩略图功能
  • PHP之图片上传类实例代码(加了缩略图)
  • php封装的单文件(图片)上传类完整实例
  • php版阿里云OSS图片上传类详解
            




    欢迎光临 时时商务社区 (http://bbs.4435.cn/) Powered by Discuz! X3.2