您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“PHP文件上傳類實(shí)例代碼分享”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
本文實(shí)例講述了PHP多文件上傳類,分享給大家供大家參考。具體如下:
<?php class Test_Upload{ protected $_uploaded = array(); protected $_destination; protected $_max = 1024000; protected $_messages = array(); protected $_permited = array( 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png' ); protected $_renamed = false; /** * * @param mix $path * */ public function __construct($path){ if (!is_dir($path) || !is_writable($path)){ throw new Exception("文件名不可寫,或者不是目錄!"); } $this->_destination = $path; $this->_uploaded = $_FILES; } /** * 移動(dòng)文件 * */ public function move(){ $filed = current($this->_uploaded); $isOk = $this->checkError($filed['name'], $filed['error']); //debug ok if ($isOk){ $sizeOk = $this->checkSize($filed['name'], $filed['size']); $typeOk = $this->checkType($filed['name'], $filed['type']); if ($sizeOk && $typeOk){ $success = move_uploaded_file($filed['tmp_name'], $this->_destination.$filed['name']); if ($success){ $this->_messages[] = $filed['name']."文件上傳成功"; }else { $this->_messages[] = $filed['name']."文件上傳失敗"; } } } } /** * 查詢messages數(shù)組內(nèi)容 * */ public function getMessages(){ return $this->_messages; } /** * 檢測(cè)上傳的文件大小 * @param mix $string * @param int $size */ public function checkSize($filename, $size){ if ($size == 0){ return false; }else if ($size > $this->_max){ $this->_messages[] = "文件超出上傳限制大小".$this->getMaxsize(); return false; }else { return true; } } /** * 檢測(cè)上傳文件的類型 * @param mix $filename * @param mix $type */ protected function checkType($filename, $type){ if (!in_array($type, $this->_permited)){ $this->_messages[] = "該文件類型是不被允許的上傳類型"; return false; }else { return true; } } /** * 獲取文件大小 * */ public function getMaxsize(){ return number_format($this->_max / 1024, 1).'KB'; } /** * 檢測(cè)上傳錯(cuò)誤 * @param mix $filename * @param int $error * */ public function checkError($filename, $error){ switch ($error){ case 0 : return true; case 1 : case 2 : $this->_messages[] = "文件過(guò)大!"; return true; case 3 : $this->_messages[] = "錯(cuò)誤上傳文件!";return false; case 4 : $this->_messages[] = "沒(méi)有選擇文件!"; return false; default : $this->_messages[] = "系統(tǒng)錯(cuò)誤!"; return false; } } } ?>
“PHP文件上傳類實(shí)例代碼分享”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(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)容。