溫馨提示×

溫馨提示×

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

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

PHP中怎么實現(xiàn)一個上傳類

發(fā)布時間:2021-06-30 15:01:18 來源:億速云 閱讀:108 作者:Leah 欄目:編程語言

PHP中怎么實現(xiàn)一個上傳類,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

<?php /**  *Fileuploadclass  *@version1.0.0(ThuAug1801:32:39CST2005)  *@authorsanshi  */  classupLoad  {  /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:00:18CST2005  *@paramstring$info文件內(nèi)容  *@paramstring$fileName生成的文件名  *@returnboolean建立成功返回true  *@deprecated  *建立html文件  */  functioncreateHtml($info,$fileName)  {  }  /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:03:09CST2005  *@returnvoid  *@deprecated  *構(gòu)造函數(shù)  */  functiondownLoad()  {}  /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:03:55CST2005  *@paramstring$fileField在表單中的字段名  *@paramstring$length限制的長度  *@returnboolean成功返回true  *@deprecated  *功能實現(xiàn)函數(shù)  */  functioninit($fileField,$length='')  {  $files=$_FILES[$fileField];  //用戶名需要改動,根據(jù)自己的實際情況做改動  $userName='sanshi';  $fileName=$files['name'];  $fileType=$files['type'];  $fileTemp=$files['tmp_name'];  $fileSize=empty($length)?($files['size']+10):$length;  $fileError=$files['error'];//這塊也許php4中沒有  //改為  //if($this->_isType($fileName)||$this->_isBig($length ))  if(!$this->_isType($fileName)||$this->_isBig($length )||$fileError!=0)  {  //print_r($files);  returnfalse;  }else{  $path=$this->_createDir($userName);//取得路徑  $createFileName=$userName."_".time();//設(shè)置當(dāng)前文件名  $createFileType=$this->getFileType($fileName);//設(shè)置文件類別  return@move_uploaded_file($fileTemp,$path.$createFileName.'.'.$createFileType)?true:false;  }  }   /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:07:43CST2005  *@paramint$length上傳限制的大小  *@returnboolean超過返回true  *@deprecated  *判斷是否超過預(yù)定大小  */  function_isBig($length)  {  $bigest='';  return$big>$bigest?true:false;  }  /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:08:55CST2005  *@paramstring$fileName文件名  *@returnstring$fileType文件后綴  *@deprecated  *取得文件后綴(只取得文件的***一個后綴名)  */  functiongetFileType($fileName)  {  returnend(explode('.',$fileName));  }  /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:10:41CST2005  *@paramstring$fileName文件名  *@paramboolean$method是否檢查多個后綴默認false  *@paramint$postFix后綴個數(shù)默認為2  *@returnboolean存在返回true  *@deprecated  *檢查文件的后綴是否在類別數(shù)組中,類別數(shù)組自己設(shè)置  *如果$method設(shè)置為true則檢查文件有幾個后綴  */  function_isType($fileName,$method='false',$postFix=2)  {  //設(shè)置類別數(shù)組  $type=array('jpeg',  'gif',  'bmp',  'exe');  $fileName=strtolower($fileName);  $fileTypeArray=explode('.',$fileName);  $fileType=end($fileTypeArray);  //判斷是否有一個文件有多個后綴  if($method)  {  if(count($fileTypeArray)>(is_int($postFix)?$postFix:2))  {  returnfalse;  }  }  returnin_array($fileType,$type);  }   /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:17:19CST2005  *@paramstring$userName  *@returnstring$path  *@deprecated  *建立目錄目錄格式年/月/日/用戶名/  *權(quán)限為755  */  function_createDir($userName)  {  $root='';  $pathSign=DIRECTORY_SEPARATOR;  $y=date('Y').$pathSign;  $m=date('m').$pathSign;  $d=date('d').$pathSign;  $path=$root.$y.$m.$d.$userName;  $dirArray=explode($pathSign,$path);  $tempDir='';  foreach($dirArrayas$dir)  {  $tempDir.=$dir.$pathSign;  $isFile=file_exists($tempDir);  clearstatcache();  if(!$isFile&&!is_dir($tempDir))  {  @mkdir($tempDir,0755);  }  }  return$path.$pathSign;  }  /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:19:32CST2005  *@param string$dirName目錄名  *@return boolean可以操作返回true  *@deprecated  *判斷操作是否在上傳目錄  */  function_isDel($dirName)  {  //注意upLoadDir,一定要與真正使用目錄相對應(yīng)  $upLoadDir='';  $upLoadDir=preg_replace('/\\//','\/',$upLoadDir);  $format="/^{$upLoadDir}/";  returnpreg_match($format,$dirName);  }  /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:25:58CST2005  *@paramstring$fileName文件名  *@returnboolean刪除文件成功返回true  *@deprecated  *刪除文件  */  functiondelFile($fileName)  {  $cur_dir=dirname(trim($fileName));  if($this->_isDel($cur_dir))  {  return@unlink($fileName)?true:false;  }else{  returnfalse;  }  }  /**  *  *@authorsanshi  *@version1.0.0ThuAug1801:27:43CST2005  *@paramstring$dieName目錄名  *@returnboolean刪除成功返回true  *@deprecated  *刪除目錄目錄下如果有文件不能刪除  */  functiondelDir($dirName)  {  if($this->_isDel($dirName)&&is_dir($dirName))  {  return@rmdir($dirName)?true:false;  }else{  returnfalse;  }  }   }  ?> <?php //使用  /*  include'upLoad.class.php';  $up=newupLoad();  if($up->init("file"))  {  echo'success';  }else{  echo'failure';  }  */  ?>

關(guān)于PHP中怎么實現(xiàn)一個上傳類問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

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

php
AI