今天做面試題,有一個文件上傳的,發(fā)覺以前做項目為了趕時間都是直接用別人的上傳類,交筆試題,怎么也不能用別人的吧,所以就寫了一個,可能很多bug,沒實際項目測試過,呵呵
<?php

/**
* 文件上傳類
* 成員變量帶*號必須要初始化
* @version 1.0
* @author howeey
*/
class FileUpload {
var $filePath; //* 文件目的路徑
var $fileField; //* 默認(rèn)$_FILES[$fileField],通過$_FILES環(huán)境變量獲取上傳文件信息
var $originName; //源文件名
var $tmpFileName; //臨時文件名
var $fileType; //文件類型(文件后綴)
var $fileSize; //文件大小
var $newFileName; //新文件名
var $allowType = array('txt','jpg','doc'); //允許上傳的文件類型
var $maxSize = 2048; //允許文件上次的最大長度

var $isUserDefName = false; //是否采用用戶自定義名
var $userDefName; //用戶定義名稱
var $isRandName = true; //是否隨機重命名
var $randName; //系統(tǒng)隨機名稱
var $errorNum = 0; //錯誤號
var $isCoverModer = true; //是否覆蓋模式
var $debug = false; //是否debug
function FileUpload($options = array()) {
//設(shè)置構(gòu)造屬性列表
$this->setOptions($options);
}
function uploadFile($filefield, $options = array()) {
//設(shè)置錯誤位
$this->setOption('errorNum',0);
//設(shè)置fileField
$this->setOption('fileField', $filefield);
//設(shè)置上傳時屬性列表
$this->setOptions($options);
//設(shè)置文件信息
$this->setFiles();
//判斷合法性
$this->checkValid();
//檢查文件路徑
$this->checkFilePath();
//設(shè)置新文件名
$this->setNewFileName();
//檢查是否出錯
if ($this->errorNum < 0) return $this->errorNum;
//上傳文件
return $this->copyFile();
}
/**
* 設(shè)置成員變量列表
*
* @param unknown_type $options
*/
function setOptions($options = array()) {
foreach ($options as $key => $val) {
if (!in_array($key, array('filePath','fileField','originName','allowType','maxSize','isUserDefName','userDefName','isRandName','randName'))) continue;
$this->setOption($key, $val);
}
}
/**
* 設(shè)置文件信息
*
*/
function setFiles() {
if ($this->getFileErrorFromFILES() != 0) {
$this->setOptions('errorNum', -1);
return ;
}
$this->setOption('originName', $this->getFileNameFromFILES());
$this->setOption('tmpFileName', $this->getTmpFileNameFromFILES());
$this->setOption('fileType', $this->getFileTypeFromFILES());
$this->setOption('fileSize', $this->getFileSizeFromFILES());
}
/**
* 設(shè)置某個成員變量
*
* @param unknown_type $key
* @param unknown_type $val
*/
function setOption($key, $val) {
$this->$key = $val;
if ($this->debug) {
echo '成員變量 '.$key.' 被設(shè)置成 '.$val.'<br>';
}
}
/**
* 設(shè)置新的文件名(根據(jù)isRandName,isUserDefName標(biāo)志分3種新文件名,隨機文件名,用戶自定義文件名,源文件名)
*
*/
function setNewFileName() {
if ($this->isRandName == false && $this->isUserDefName == false) {
//新文件名和原來文件名相同
$this->setOption('newFileName', $this->originName);
} else if ($this->isRandName == true && $this->isUserDefName == false) {
//產(chǎn)生隨機文件名
$this->setOption('newFileName', $this->proRandName().'.'.$this->fileType);
} else if ($this->isRandName == false && $this->isUserDefName == true) {
//產(chǎn)生用戶自定義用戶名
$this->setOption('newFileName', $this->userDefName);
} else {
$this->setOption('errorNum', -4);
}
}
/**
* 判斷上傳文件的合法性
*
*/
function checkValid() {
//判斷文件大小
$this->checkFileSize();
//判斷文件類型
$this->checkFileType();
}
/**
* 檢查文件類型
*
*/
function checkFileType() {
if (!in_array($this->fileType, $this->allowType)) $this->setOption('errorNum', -2);
return $this->errorNum;
}
/**
* 判斷文件大小
*
*/
function checkFileSize() {
if ($this->fileSize > $this->maxSize) $this->setOption('errorNum', -3);
return $this->errorNum;
}
/**
* 判斷文件路徑
*
*/
function checkFilePath() {
if (!file_exists($this->filePath)) {
if ($this->isCoverModer) {
//如果是覆蓋模式,建立路徑
$this->makePath();
} else {
$this->setOption('errorNum', -6);
}
}
}
/**
* 產(chǎn)生隨機文件名
*
*/
function proRandName() {
$tmpStr = "abcdefghijklmnopqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
$str = "";
for ($i=0; $i<8; $i++) {
$num = rand(0, strlen($tmpStr));
$str .= $tmpStr[$num];
}
return $str;
}
/**
* 建立文件路徑
*
*/
function makePath() {
if (!@mkdir($this->filePath, 0755)) {
$this->setOption('errorNum', -7);
}
}
/**
* 拷貝文件到指定目錄
*
* @return unknown
*/
function copyFile() {
$filePath = $this->filePath;
if ($filePath[strlen($filePath)-1] != '/') {
$filePath .= '/';
}
$filePath .= $this->newFileName;
if (!@move_uploaded_file($this->tmpFileName, $filePath)) {
$this->setOption('errorNum', -5);
}
return $this->errorNum;
}
/**
* 從環(huán)境變量$_FILES獲取文件錯誤
*
*/
function getFileErrorFromFILES() {
return $_FILES[$this->fileField]['error'];
}
/**
* 從環(huán)境變量$_FILES獲取文件類型
*
*/
function getFileTypeFromFILES() {
$str = $_FILES[$this->fileField]['name'];
$aryStr = split("\.", $str);
$ret = strtolower($aryStr[count($aryStr)-1]);
return $ret;
}
/**
* 從環(huán)境變量$_FILES獲取文件名
*
*/
function getFileNameFromFILES() {
return $_FILES[$this->fileField]['name'];
}
/**
* 從環(huán)境變量$_FILES獲取臨時變量名
*
*/
function getTmpFileNameFromFILES() {
return $_FILES[$this->fileField]['tmp_name'];
}
/**
* 從環(huán)境變量$_FILES獲取文件大小
*
*/
function getFileSizeFromFILES() {
return $_FILES[$this->fileField]['size'];
}
function getErrorMsg() {
$str = "上傳文件出錯 : ";
switch ($this->errorNum) {
case -1:
$str .= "未知錯誤";
break;
case -2:
$str .= "未允許類型";
break;
case -3:
$str .= "文件過大";
break;
case -4:
$str .= "產(chǎn)生文件名出錯";
break;
case -5:
$str .= "上傳失敗";
break;
case -6:
$str .= "目錄不存在";
break;
case -7:
$str .= "建立目錄失敗";
break;
}
return $str;
}
/**
* 設(shè)置是否debug
*
* @param unknown_type $debug
*/
function setDebug($debug) {
$this->debug = $debug;
}
}

?>

<?
echo '<br>';
$tmp = new FileUpload(array('filePath'=>'./default'));
foreach ($_FILES as $key => $val) {
$res = $tmp->uploadFile($key);
if ($res < 0) echo $tmp->getErrorMsg().'<br>';
else echo '文件上傳成功<br>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>多文件上傳demo</title>
</head>


<script language="javascript">

var trIndex = 0;
var tmpIndex = 0;


function addItem()
{
trIndex++;
tmpIndex++;
var containerObj = document.getElementById("container0");
var tr = containerObj.insertRow(trIndex);
var td = tr.insertCell(0);
str = "userfile" + String(trIndex);
alert(str);
td.innerHTML = "<input id=\""+str+"\"name=\""+str+"\" type=\"file\"> " + " <input name=\"del\" type=\"button\" value=\"刪除\" onclick=\"delItem("+String(trIndex)+");\"> ";
}


function delItem(trnum)
{
var containerObj = document.getElementById("container0");
var tr = containerObj.deleteRow(trIndex);
trIndex--;
}

</script>

<body>

<form id="form0" enctype="multipart/form-data" action="FileUpload.php" method="POST">
<table id="container0" width="100%" border="0" cellspacing="5" cellpadding="0">
<tr >
<td > <p>
<input id="fu0" type="hidden" name="MAX_FILE_SIZE" value="300000000" />
<input name="userfile" type="file" />
</p>
</td>
</tr>
<tr >
<td >
<p>
<input name="add" type="button" value="添加" onclick="addItem();" />
<input type="submit" value="上傳" />
</p>
</td>
</tr>
</table>

</form>

</body>
</html>

posted on 2008-04-12 11:47
豪 閱讀(1515)
評論(0) 編輯 收藏 引用 所屬分類:
PHP之路