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 "没有图片数据可上传";
}
?>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!