php 檔案上傳類

來源:互聯網
上載者:User

標籤:ade   rand   ble   檔案上傳   tip   表單   top   hid   load   

<?php
/**
file: fileupload.class.php 檔案上傳類FileUpload
本類的執行個體對象用於處理上傳檔案,可以上傳一個檔案,也可同時處理多個檔案上傳
*/
class FileUpload {
private $path = "./uploads"; //上傳檔案儲存的路徑
private $allowtype = array(‘jpg‘,‘gif‘,‘png‘); //設定限制上傳檔案的類型
private $maxsize = 1000000; //限制檔案上傳大小(位元組)
private $israndname = true; //設定是否隨機重新命名檔案, false不隨機

private $originName; //源檔案名稱
private $tmpFileName; //臨時檔案名稱
private $fileType; //檔案類型(檔案尾碼)
private $fileSize; //檔案大小
private $newFileName; //新檔案名稱
private $errorNum = 0; //錯誤號碼
private $errorMess=""; //錯誤報表訊息

/**
* 用於設定成員屬性($path, $allowtype,$maxsize, $israndname)
* 可以通過連貫操作一次設定多個屬性值
*@param string $key 成員屬性名(不區分大小寫)
*@param mixed $val 為成員屬性設定的值
*@return object 返回自己對象$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;
}

/**
* 調用該方法上傳檔案
* @param string $fileFile 上傳檔案的表單名稱
* @return bool 如果上傳成功返回數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‘];

/* 如果是多個檔案上傳則$file["name"]會是一個數組 */
if(is_Array($name)){
$errors=array();
/*多個檔案上傳則迴圈處理 , 這個迴圈只有檢查上傳檔案的作用,並沒有真正上傳 */
for($i = 0; $i < count($name); $i++){
/*設定檔案資訊 */
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;
}
/* 如果有問題,則重新初使化屬性 */
if(!$return)
$this->setFiles();
}

if($return){
/* 存放所有上傳後檔案名稱的變數數組 */
$fileNames = array();
/* 如果上傳的多個檔案都是合法的,則通過銷魂迴圈向伺服器上傳檔案 */
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;
/*上傳單個檔案處理方法*/
} else {
/* 設定檔案資訊 */
if($this->setFiles($name,$tmp_name,$size,$error)) {
/* 上傳之前先檢查一下大小和類型 */
if($this->checkFileSize() && $this->checkFileType()){
/* 為上傳檔案設定新檔案名稱 */
$this->setNewFileName();
/* 上傳檔案 返回0為成功, 小於0都為錯誤 */
if($this->copyFile()){
return true;
}else{
$return=false;
}
}else{
$return=false;
}
} else {
$return=false;
}
//如果$return為false, 則出錯,將錯誤資訊儲存在屬性errorMess中
if(!$return)
$this->errorMess=$this->getError();

return $return;
}
}

/**
* 擷取上傳後的檔案名稱
* @param void 沒有參數
* @return string 上傳後,新檔案的名稱, 如果是多檔案上傳返回數組
*/
public function getFileName(){
return $this->newFileName;
}

/**
* 上傳失敗後,調用該方法則返回,上傳出錯資訊
* @param void 沒有參數
* @return string 返回上傳檔案出錯的資訊報告,如果是多檔案上傳返回數組
*/
public function getErrorMsg(){
return $this->errorMess;
}

/* 設定上傳出錯資訊 */
private function getError() {
$str = "上傳檔案<font color=‘red‘>{$this->originName}</font>時出錯 : ";
switch ($this->errorNum) {
case 4: $str .= "沒有檔案被上傳"; break;
case 3: $str .= "檔案只有部分被上傳"; break;
case 2: $str .= "上傳檔案的大小超過了HTML表單中MAX_FILE_SIZE選項指定的值"; break;
case 1: $str .= "上傳的檔案超過了php.ini中upload_max_filesize選項限制的值"; break;
case -1: $str .= "未允許類型"; break;
case -2: $str .= "檔案過大,上傳的檔案不能超過{$this->maxsize}個位元組"; break;
case -3: $str .= "上傳失敗"; break;
case -4: $str .= "建立存放上傳檔案目錄失敗,請重新指定上傳目錄"; break;
case -5: $str .= "必須指定上傳檔案的路徑"; break;
default: $str .= "未知錯誤";
}
return $str.‘<br>‘;
}

/* 設定和$_FILES有關的內容 */
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;
}

/* 為單個成員屬性設定值 */
private function setOption($key, $val) {
$this->$key = $val;
}

/* 設定上傳後的檔案名稱 */
private function setNewFileName() {
if ($this->israndname) {
$this->setOption(‘newFileName‘, $this->proRandName());
} else{
$this->setOption(‘newFileName‘, $this->originName);
}
}

/* 檢查上傳的檔案是否是合法的類型 */
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 ([email protected]($this->path, 0755)) {
$this->setOption(‘errorNum‘, -4);
return false;
}
}
return true;
}

/* 設定隨機檔案名稱 */
private function proRandName() {
$fileName = date(‘YmdHis‘)."_".rand(100,999);
return $fileName.‘.‘.$this->fileType;
}

/* 複製上傳檔案到指定的位置 */
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;
}
}
}

 

 

 

<?php
//包含一個檔案上傳類中的上傳類
include "fileupload.class.php";

$up = new fileupload;
//設定屬性(上傳的位置, 大小, 類型, 名是是否要隨機產生)
$up -> set("path", "./images/");
$up -> set("maxsize", 2000000);
$up -> set("allowtype", array("gif", "png", "jpg","jpeg"));
// $up -> set("israndname", false);

//使用對象中的upload方法, 就可以上傳檔案, 方法需要傳一個上傳表單的名子 pic, 如果成功返回true, 失敗返回false
if($up -> upload("pic")) {
echo ‘<pre>‘;
//擷取上傳後檔案名稱子
var_dump($up->getFileName());
echo ‘</pre>‘;

} else {
echo ‘<pre>‘;
//擷取上傳失敗以後的錯誤提示
var_dump($up->getErrorMsg());
echo ‘</pre>‘;
}
?>

 

 

 

 

<form action="upload.php" method="post" enctype="multipart/form-data" >
name: <input type="text" name="username" value="" /><br>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
up pic: <input type="file" name="pic[]" value=""><br>
up pic: <input type="file" name="pic[]" value=""><br>
up pic: <input type="file" name="pic[]" value=""><br>
up pic: <input type="file" name="pic[]" value=""><br>

<input type="submit" value="upload" /><br>

</form>

 

php 檔案上傳類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.