1、upload.php頁面代碼
<!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>無標(biāo)題文檔</title>
</head>
<body>
<form?action="doaction_new.php"?method="post"?enctype="multipart/form-data">
請選擇要上傳的文件:
<input?type="file"?name="myFile"/>
<input?type="submit"?value="上傳文件"/>
</form>
</body>
</html>
2、doaction_new.php頁面的代碼
<?php
header('content-type:text/html;charset=gb2312');
require_once?'upload.class.php';
//print_r($_FILES);exit;
$upload=new?upload();
$dest=$upload->uploadFile();
echo?$dest;
3、upload.class.php頁面的代碼
<?php
class?upload{
protected?$fileName;
protected?$maxSize;
protected?$allowMime;
protected?$allowExt;
protected?$uploadPath;
protected?$imgFlag;
protected?$fileInfo;
protected?$error;
protected?$ext;
public?function?__construct($fileName='myFile',$uploadPath='./uploads',$imgFlag=true,$maxSize=5242880,$allowExt=array('jpeg','jpg','png','gif','bmp'),$allowMime=array('image/pjpeg','image/jpeg','image/jpg','image/png','image/gif')){
$this->fileName=$fileName;
$this->maxSize=$maxSize;
$this->allowMime=$allowMime;
$this->allowExt=$allowExt;
$this->uploadPath=$uploadPath;
$this->imgFlag=$imgFlag;
$this->fileInfo=$_FILES[$this->fileName];
}
/**檢測上傳文件是否有錯(cuò)**/
protected?function?checkError(){
//var_dump($this->fileInfo);exit;
//print_r($this->fileInfo);exit;
if(!is_null($this->fileInfo)){
if($this->fileInfo['error']>0){
var_dump($this->fileInfo);exit;
if($this->fileInfo['error']>0){
switch($this->fileInfo['error']){
case?1;
???$this->error='超過了PHP配置文件中upload_max_filesize選項(xiàng)的值';
???break;
case?2;
???$this->error='超過了表單中Max_file_size設(shè)置的值';
???break;
case?3;
???$this->error='文件部分被上傳';
???break;
case?4;
???$this->error='沒有選擇上傳文件';
???break;
case?6;
???$this->error='沒有找到臨時(shí)目錄';
???break;
case?7;
???$this->error='文件不可寫';
???break;
case?8;
???$this->error='由于PHP的擴(kuò)展程序中斷上傳';
???break;
}
return?false;
}else{
echo?aa;
?????return?true;
?????}
???}else{
? $this->error='文件上傳出錯(cuò)';
? return?false;
?????}
}
/**檢查上傳文件的大小**/
protected?function?checkSize(){
if($this->fileInfo['size']>$this->maxSize){
$this->error='上傳文件過大';
return?false;
}
return?true;
}
/**檢測擴(kuò)展名**/
protected?function?checkExt(){
$this->ext=strtolower(pathinfo($this->fileInfo['name'],PATHINFO_EXTENSION));
if(!in_array($this->ext,$this->allowExt)){
$this->error='不允許的擴(kuò)展名';
return?false;
}
return?true;
}
/**檢測文件的類型**/
protected?function?checkMime(){
????if(!in_array($this->fileInfo['type'],$this->allowMime)){
$this->error='不允許的文件類型';
return?false;
}
return?true;
}
/**檢測是否是真實(shí)圖片**/
protected?function?checkTrueImg(){
????if($this->imgFlag){
if(!@getimagesize($this->fileInfo['tmp_name'])){
$this->error='不是真實(shí)的照片';
return?false;
}
return?true;
}
}
/**檢測是否通過HTTPPost方式上傳**/
protected?function?checkHTTPPost(){
????if(!is_uploaded_file($this->fileInfo['tmp_name'])){
$this->error='文件不是通過HTTPPost方式上傳的';
return?false;
}
return?true;
}
/**顯示錯(cuò)誤**/
protected?function?showError(){
??exit('<sapn?style="color:red">'.$this->error.'</span>');
}
/**檢測目錄不存在則創(chuàng)建**/
protected?function?checkUploadPath(){
if(!file_exists($this->uploadPath)){
mkdir($this->uploadPath,0777,true);
}
}
/**產(chǎn)生唯一字符串當(dāng)做文件名**/
protected?function?getUniName(){
return?md5(uniqid(microtime(true),true));
}
/**上傳文件**/
public?function?uploadFile(){
if($this->checkError()&&$this->checkSize()&&$this->checkExt()&&$this->checkMime()&&$this->checkTrueImg()&&$this->checkHTTPPost()){
$this->checkUploadPath();
$this->uniName=$this->getUniName();
$this->destination=$this->uploadPath.'/'.$this->uniName.'.'.$this->ext;
if(@move_upload_file($this->fileInfo['tmp_name'],$this->destination)){
return?$this->destination;
}else{
$this->error='文件移動(dòng)失敗';
$this->showError();
}
}else{
$this->showError();
????}
}
}
2014-11-19
對,這個(gè)你需要調(diào)試,看看到底是哪部出錯(cuò)啦,調(diào)試也是進(jìn)步的過程
^-^...
2014-11-19
其實(shí)有時(shí)候肯定是你自己太粗心的問題嘛~~~~我也很多時(shí)候是這樣子的呢~
沒關(guān)系,慢慢找哈~