PHP實現(xiàn)原生態(tài)圖片上傳封裝類方法
PHP圖片上傳類,經(jīng)典方式,不過上傳效率還算可以,我自己用過的一個類,當(dāng)時對這個類做了些修改,以滿足自己特定功能的需要,對PHP熟悉的,可對這個上傳類做優(yōu)化和修改,后附有調(diào)用方法,讓PHP開發(fā)者上傳圖片輕松容易就做到,先上類代碼:
<?php
class FileUpload_Single
{
//user define -------------------------------------
var $accessPath ;
var $fileSize=200;
var $defineTypeList="jpg|jpeg|gif|bmp";//string jpg|gif|bmp ...
var $filePrefix= "useruplod_";//上傳后的文件名前綴,可設(shè)置為空
var $changNameMode;//圖片改名的規(guī)則,暫時只有三類,值范圍 : 0 至 2 任一值
var $uploadFile;//array upload file attribute
var $newFileName;
var $error;
function TODO()
{//main 主類:設(shè)好參數(shù),可以直接調(diào)用
$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以內(nèi)。');history.back()</script>");
return false;
}
if ( ! $this -> MoveFileToNewPath() )
{
$pass = false;
$this -> error .= die("<script language=\"javascript\">alert('上傳失敗!文件移動發(fā)生錯誤!');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();
//檢查目錄是否存在,不存在則創(chuàng)建,當(dāng)時我用的時候添加了這個功能,覺得沒用的就注釋掉吧
/*
$isFile = file_exists( $this -> accessPath);
clearstatcache();
if( ! $isFile && !is_dir($this -> accessPath) )
{
echo $this -> accessPath;
@mkdir($this -> accessPath);
}*/
$array_dir=explode("/",$this -> accessPath);//把多級目錄分別放到數(shù)組中
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上傳類的調(diào)用方法,PHP代碼如下:
<?php
include 'up.class.php';//加載PHP上傳類文件
if (empty($HTTP_POST_FILES['image_file']['tmp_name']))//判斷接收數(shù)據(jù)是否為空
{
$tmp = new FileUpload_Single;
$tmp -> accessPath ='upload';//圖片上傳的目錄,這里是當(dāng)前目錄下的upload目錄,可自己修改
if ( $tmp -> TODO() )
{
$filename=$tmp -> newFileName;//生成的文件名
echo "圖片上傳成功,路徑為:upload/".$filename;
}else{
echo $tmp -> error;
}
}
else{
echo "沒有圖片數(shù)據(jù)可上傳";
}
?>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
php根據(jù)地址獲取百度地圖經(jīng)緯度的實例方法
在本篇文章里小編給大家整理了關(guān)于php根據(jù)地址獲取百度地圖經(jīng)緯度的實例方法,有需要的朋友們可以學(xué)習(xí)下。2019-09-09
Win2003+apache+PHP+SqlServer2008 配置生產(chǎn)環(huán)境
因項目的特殊需要,需要用php鏈接sql2008數(shù)據(jù)庫,為此,光這個環(huán)境問題就折磨了我好久,現(xiàn)在記錄下來過程,分享給大家2014-07-07
PHP全局變量中的global與$GLOBALS的區(qū)別和用法小結(jié)
這篇文章主要介紹了PHP全局變量中的global與$GLOBALS的區(qū)別和用法小結(jié),global的作用就相當(dāng)于傳遞參數(shù),在函數(shù)外部聲明的變量,如果在函數(shù)內(nèi)想要使用,就用global來聲明該變量,這樣就相當(dāng)于把該變量傳遞進(jìn)來了,就可以引用該變量了,需要的朋友可以參考下2023-10-10
php使用mb_check_encoding檢查字符串在指定的編碼里是否有效
本文說的是PHP使用mb_check_encoding檢查字符串在指定的編碼里是否有效的實例2013-11-11
PHP實現(xiàn)微信支付(jsapi支付)和退款(無需集成支付SDK)流程教程詳解
本篇文章給大家介紹PHP實現(xiàn)微信支付(jsapi支付)和退款(無需集成支付SDK)流程教程詳解,使用了微信官方給的php版本的sdk,但是在使用過程中有很多問題,今天給大家講講不集成支付SDK直接調(diào)用支付接口實現(xiàn)支付和退款,感興趣的朋友一起看看吧2018-03-03
laravel實現(xiàn)查詢最后執(zhí)行的一條sql語句的方法
今天小編就為大家分享一篇laravel實現(xiàn)查詢最后執(zhí)行的一條sql語句的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10

