这篇文章主要为大家详细介绍了使用PHP怎么封装一个图片上传类,文中示例代码介绍的非常详细,具有一定的参考价值,发现的小伙伴们可以参考一下:
php是一个嵌套的缩写名称,指的是英文超级文本预处理语言(php:Hypertext Preprocessor)的缩写,它的语法混合了C、Java、Perl以及php自创新的语法,主要用来做网站开发,许多小型网站都用php开发,因为php是开源的,从而使得php经久不衰。
<?php
class FileUpload_Single
{
//user define -------------------------------------
var $accessPath ;
var $fileSize=200;
var $defineTypeList="jpg|jpeg|gif|bmp";//string jpg|gif|bmp ...
var $filePrefix= "useruplod_";//上传后的文件名前缀,可设置为空
var $changNameMode;//图片改名的规则,暂时只有三类,值范围 : 0 至 2 任一值
var $uploadFile;//array upload file attribute
var $newFileName;
var $error;
function TODO()
{//main 主类:设好参数,可以直接调用
$pass = true ;
if ( ! $this -> GetFileAttri() )
{
$pass = false;
}
if( ! $this -> CheckFileMIMEType() )
{
$pass = false;
$this -> error .= die("<script language=\"javascript\">alert('图片类型不正确,允许格式:jpg|jpeg|gif|bmp。');history.back()</script>");
}
if( ! $this -> CheckFileAttri_size() )
{
$pass = false;
$this -> error .= die("<script language=\"javascript\">alert('上传的文件太大,请确保在200K以内。');history.back()</script>");
return false;
}
if ( ! $this -> MoveFileToNewPath() )
{
$pass = false;
$this -> error .= die("<script language=\"javascript\">alert('上传失败!文件移动发生错误!');history.back()</script>");
}
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<count($array_dir);$i++){
$path .= $array_dir[$i]."/";
if(!file_exists($path)){
mkdir($path);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
if ( move_uploaded_file( $this -> 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代码如下:
<?php
include 'up.class.php';//加载PHP上传类文件
if (empty($HTTP_POST_FILES['image_file']['tmp_name']))//判断接收数据是否为空
{
$tmp = new FileUpload_Single;
$tmp -> accessPath ='upload';//图片上传的目录,这里是当前目录下的upload目录,可自己修改
if ( $tmp -> TODO() )
{
$filename=$tmp -> newFileName;//生成的文件名
echo "图片上传成功,路径为:upload/".$filename;
}else{
echo $tmp -> error;
}
}
else{
echo "没有图片数据可上传";
}
?>
以上就是亿速云小编为大家收集整理的使用PHP怎么封装一个图片上传类,如何觉得亿速云网站的内容还不错,欢迎将亿速云网站推荐给身边好友。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。