溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用PHP怎么封裝一個(gè)圖片上傳類

發(fā)布時(shí)間:2021-02-25 14:40:15 來源:億速云 閱讀:147 作者:戴恩恩 欄目:開發(fā)技術(shù)

這篇文章主要為大家詳細(xì)介紹了使用PHP怎么封裝一個(gè)圖片上傳類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,發(fā)現(xiàn)的小伙伴們可以參考一下:

php有什么用

php是一個(gè)嵌套的縮寫名稱,指的是英文超級(jí)文本預(yù)處理語(yǔ)言(php:Hypertext Preprocessor)的縮寫,它的語(yǔ)法混合了C、Java、Perl以及php自創(chuàng)新的語(yǔ)法,主要用來做網(wǎng)站開發(fā),許多小型網(wǎng)站都用php開發(fā),因?yàn)閜hp是開源的,從而使得php經(jīng)久不衰。

<?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ī)則,暫時(shí)只有三類,值范圍 : 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('上傳的文件太大,請(qǐng)確保在200K以內(nèi)。');history.back()</script>");

  return false;

}

if ( ! $this -> MoveFileToNewPath() )

{
  $pass = false;
  $this -> error .= die("<script language=\"javascript\">alert('上傳失?。∥募苿?dòng)發(fā)生錯(cuò)誤!');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)時(shí)我用的時(shí)候添加了這個(gè)功能,覺得沒用的就注釋掉吧

 /*

 $isFile = file_exists( $this -> accessPath);

 clearstatcache();

  if( ! $isFile && !is_dir($this -> accessPath) )

  {

    echo $this -> accessPath;

  @mkdir($this -> accessPath);

  }*/

$array_dir=explode("/",$this -> accessPath);//把多級(jí)目錄分別放到數(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ù)可上傳";
}
?>

以上就是億速云小編為大家收集整理的使用PHP怎么封裝一個(gè)圖片上傳類,如何覺得億速云網(wǎng)站的內(nèi)容還不錯(cuò),歡迎將億速云網(wǎng)站推薦給身邊好友。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI