php上传图片限制类型,php,_使用php的图片上传类进行图片上传,总是提示:上传文件时出错 : 未允许类型 。都是默认的配置,php - phpStudy...
使用php的圖片上傳類(lèi)進(jìn)行圖片上傳,總是提示:上傳文件時(shí)出錯(cuò) : 未允許類(lèi)型 。都是默認(rèn)的配置
使用php的圖片上傳類(lèi)進(jìn)行圖片上傳,總是提示:上傳文件時(shí)出錯(cuò) : 未允許類(lèi)型 。都是默認(rèn)的配置
$uploadImg= new FileUpload;
if($uploadImg ->upload("head")){
echo '
';var_dump($uploadImg ->getFileName());
echo '
';}else{
echo '
';var_dump($uploadImg ->getErrorMsg());
echo '
';}
class FileUpload {
private $path = "./uploads"; //上傳文件保存的路徑
private $allowtype = array('jpg','jpeg','gif','png'); //設(shè)置限制上傳文件的類(lèi)型
private $maxsize = 1000000; //限制文件上傳大小(字節(jié))
private $israndname = true; //設(shè)置是否隨機(jī)重命名文件, false不隨機(jī)
private $originName; //源文件名
private $tmpFileName; //臨時(shí)文件名
private $fileType; //文件類(lèi)型(文件后綴)
private $fileSize; //文件大小
private $newFileName; //新文件名
private $errorNum = 0; //錯(cuò)誤號(hào)
private $errorMess=""; //錯(cuò)誤報(bào)告消息
/**
* 用于設(shè)置成員屬性($path, $allowtype,$maxsize, $israndname)
* 可以通過(guò)連貫操作一次設(shè)置多個(gè)屬性值
*@param string $key 成員屬性名(不區(qū)分大小寫(xiě))
*@param mixed $val 為成員屬性設(shè)置的值
*@return object 返回自己對(duì)象$this,可以用于連貫操作
*/
function set($key, $val){
$key = strtolower($key);
if( array_key_exists( $key, get_class_vars(get_class($this) ) ) ){
$this->setOption($key, $val);
}
return $this;
}
/**
* 調(diào)用該方法上傳文件
* @param string $fileFile 上傳文件的表單名稱(chēng)
* @return bool 如果上傳成功返回?cái)?shù)true
*/
function upload($fileField) {
$return = true;
/* 檢查文件路徑是滯合法 */
if( !$this->checkFilePath() ) {
$this->errorMess = $this->getError();
return false;
}
/* 將文件上傳的信息取出賦給變量 */
$name = $_FILES[$fileField]['name'];
$tmp_name = $_FILES[$fileField]['tmp_name'];
$size = $_FILES[$fileField]['size'];
$error = $_FILES[$fileField]['error'];
/* 如果是多個(gè)文件上傳則$file["name"]會(huì)是一個(gè)數(shù)組 */
if(is_Array($name)){
$errors=array();
/*多個(gè)文件上傳則循環(huán)處理 , 這個(gè)循環(huán)只有檢查上傳文件的作用,并沒(méi)有真正上傳 */
for($i = 0; $i < count($name); $i++){
/*設(shè)置文件信息 */
if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i] )) {
if(!$this->checkFileSize() || !$this->checkFileType()){
$errors[] = $this->getError();
$return=false;
}
}else{
$errors[] = $this->getError();
$return=false;
}
/* 如果有問(wèn)題,則重新初使化屬性 */
if(!$return)
$this->setFiles();
}
if($return){
/* 存放所有上傳后文件名的變量數(shù)組 */
$fileNames = array();
/* 如果上傳的多個(gè)文件都是合法的,則通過(guò)銷(xiāo)魂循環(huán)向服務(wù)器上傳文件 */
for($i = 0; $i < count($name); $i++){
if($this->setFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i] )) {
$this->setNewFileName();
if(!$this->copyFile()){
$errors[] = $this->getError();
$return = false;
}
$fileNames[] = $this->newFileName;
}
}
$this->newFileName = $fileNames;
}
$this->errorMess = $errors;
return $return;
/*上傳單個(gè)文件處理方法*/
} else {
/* 設(shè)置文件信息 */
if($this->setFiles($name,$tmp_name,$size,$error)) {
/* 上傳之前先檢查一下大小和類(lèi)型 */
if($this->checkFileSize() && $this->checkFileType()){
/* 為上傳文件設(shè)置新文件名 */
$this->setNewFileName();
/* 上傳文件 返回0為成功, 小于0都為錯(cuò)誤 */
if($this->copyFile()){
return true;
}else{
$return=false;
}
}else{
$return=false;
}
} else {
$return=false;
}
//如果$return為false, 則出錯(cuò),將錯(cuò)誤信息保存在屬性errorMess中
if(!$return)
$this->errorMess=$this->getError();
return $return;
}
}
/**
* 獲取上傳后的文件名稱(chēng)
* @param void 沒(méi)有參數(shù)
* @return string 上傳后,新文件的名稱(chēng), 如果是多文件上傳返回?cái)?shù)組
*/
public function getFileName(){
return $this->newFileName;
}
/**
* 上傳失敗后,調(diào)用該方法則返回,上傳出錯(cuò)信息
* @param void 沒(méi)有參數(shù)
* @return string 返回上傳文件出錯(cuò)的信息報(bào)告,如果是多文件上傳返回?cái)?shù)組
*/
public function getErrorMsg(){
return $this->errorMess;
}
/* 設(shè)置上傳出錯(cuò)信息 */
private function getError() {
$str = "上傳文件{$this->originName}時(shí)出錯(cuò) : ";
switch ($this->errorNum) {
case 4: $str .= "沒(méi)有文件被上傳"; break;
case 3: $str .= "文件只有部分被上傳"; break;
case 2: $str .= "上傳文件的大小超過(guò)了HTML表單中MAX_FILE_SIZE選項(xiàng)指定的值"; break;
case 1: $str .= "上傳的文件超過(guò)了php.ini中upload_max_filesize選項(xiàng)限制的值"; break;
case -1: $str .= "未允許類(lèi)型"; break;
case -2: $str .= "文件過(guò)大,上傳的文件不能超過(guò){$this->maxsize}個(gè)字節(jié)"; break;
case -3: $str .= "上傳失敗"; break;
case -4: $str .= "建立存放上傳文件目錄失敗,請(qǐng)重新指定上傳目錄"; break;
case -5: $str .= "必須指定上傳文件的路徑"; break;
default: $str .= "未知錯(cuò)誤";
}
return $str.'
';
}
/* 設(shè)置和$_FILES有關(guān)的內(nèi)容 */
private function setFiles($name="", $tmp_name="", $size=0, $error=0) {
$this->setOption('errorNum', $error);
if($error)
return false;
$this->setOption('originName', $name);
$this->setOption('tmpFileName',$tmp_name);
$aryStr = explode(".", $name);
$this->setOption('fileType', strtolower($aryStr[count($aryStr)-1]));
$this->setOption('fileSize', $size);
return true;
}
/* 為單個(gè)成員屬性設(shè)置值 */
private function setOption($key, $val) {
$this->$key = $val;
}
/* 設(shè)置上傳后的文件名稱(chēng) */
private function setNewFileName() {
if ($this->israndname) {
$this->setOption('newFileName', $this->proRandName());
} else{
$this->setOption('newFileName', $this->originName);
}
}
/* 檢查上傳的文件是否是合法的類(lèi)型 */
private function checkFileType() {
if (in_array(strtolower($this->fileType), $this->allowtype)) {
return true;
}else {
$this->setOption('errorNum', -1);
return false;
}
}
/* 檢查上傳的文件是否是允許的大小 */
private function checkFileSize() {
if ($this->fileSize > $this->maxsize) {
$this->setOption('errorNum', -2);
return false;
}else{
return true;
}
}
/* 檢查是否有存放上傳文件的目錄 */
private function checkFilePath() {
if(empty($this->path)){
$this->setOption('errorNum', -5);
return false;
}
if (!file_exists($this->path) || !is_writable($this->path)) {
if (!@mkdir($this->path, 0755)) {
$this->setOption('errorNum', -4);
return false;
}
}
return true;
}
/* 設(shè)置隨機(jī)文件名 */
private function proRandName() {
$fileName = date('YmdHis')."_".rand(100,999);
return $fileName.'.'.$this->fileType;
}
/* 復(fù)制上傳文件到指定的位置 */
private function copyFile() {
if(!$this->errorNum) {
$path = rtrim($this->path, '/').'/';
$path .= $this->newFileName;
if (@move_uploaded_file($this->tmpFileName, $path)) {
return true;
}else{
$this->setOption('errorNum', -3);
return false;
}
} else {
return false;
}
}
}
相關(guān)閱讀:
node.js里面運(yùn)行js后沒(méi)有反應(yīng)?
SSH Secure Shell中文亂碼
如何學(xué)習(xí) Mac 開(kāi)發(fā)?
昏了.... mysql 怎么 where 2個(gè)字段同時(shí)成立 要怎么寫(xiě)?
用Python匹配HTML tag的時(shí)候,<.>和<.>有什么區(qū)別?
幾行代碼判斷當(dāng)前時(shí)間是否是今天的下午,好像和預(yù)想的不一樣
安裝了sql server 2008后 找不到 sql server management studio
為什么同一個(gè)QT程序在不同機(jī)器上有不同的外觀。
為什么 React Native 可以做到從服務(wù)器更新 App 端代碼?
angular指令中的屬性可以去掉嗎?
圖片檢索功能
c++ 如何啟動(dòng)兩個(gè)exe 文件?
today widget調(diào)試沒(méi)反應(yīng)怎么解決?
在javascript中正則表達(dá)式/(.)^/的匹配規(guī)則是什么
python執(zhí)行mysql語(yǔ)句發(fā)生的時(shí)間錯(cuò)誤問(wèn)題
微信創(chuàng)建菜單api,返回 40019: invalid button key size
用node.js的http-server出錯(cuò)?
celery中task的eta countdown的實(shí)現(xiàn)方式
使用php的swoole擴(kuò)展編寫(xiě)后端接口和使用其他語(yǔ)言寫(xiě)的后端各有什么特色?
Android 啟動(dòng)界面
總結(jié)
以上是生活随笔為你收集整理的php上传图片限制类型,php,_使用php的图片上传类进行图片上传,总是提示:上传文件时出错 : 未允许类型 。都是默认的配置,php - phpStudy...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql 之后,装完MySQL之后的一
- 下一篇: matlab读取表格读成mat文件,MA