<代码>类上传{//错误信息 私人errorNo美元; 私人errorMsg美元;//文件类型 私人ext美元;//允许的文件类型 私人allowExt美元;//文件的大小 私人美元大小;//允许的文件大小 私人allowSize美元;//存放图片的主文件名称 私人dir美元;//子文件夹名称 私人dirSec美元;//临时文件名 私人tmpName美元;//分隔符 const DS=DIRECTORY_SEPARATOR; 公共函数__construct(文件,dir=吧洗?美元$ allowExt=(jpg, jpeg、gif, png的],allowSize=2097152美元){ $ this→errorNo=$文件(“错误”); ext=$ $ this→文件(“名字”); $ this→大?$文件(“大小”); $ this→tmpName=$文件(“tmp_name”); $ this→dir=dir美元; $ this→allowExt=$ allowExt; $ this→allowSize=$ allowSize; } 公共函数上传(){ 如果(! $ this→checkFile ()) { 返回$ this→errorMsg; } 如果(! $ this→createDir ()) { 返回$ this→errorMsg; }; echo $ this→移动文件(); } 私有函数checkFile () { 如果(! $ this→checkError ()) { $ this→errorMsg='文件错误,无法上传!”; 返回错误; } 如果(! $ this→checkExt ()) { $ this→errorMsg='不是图片,无法上传!”; 返回错误; } 如果(! $ this→checkSize ()) { $ this→errorMsg='文件超过指定大小,无法上传的; 返回错误; } 返回true; }//检查文件错误 私有函数checkError () { if ($ this→errorNo !=0) { 返回错误; } 返回true; }//检查文件类型 私有函数checkExt () { 如果(! in_array (pathinfo ($ this→ext)(“扩展”),$ this→allowExt)) { 返回错误; } 返回true; }//检查文件大小 私有函数checkSize () { if ($ this→大小比;$ this→allowSize) { 返回错误; } 返回true; }//创建文件夹 私有函数createDir () { $ this→dirSec=$这→dir.self: DS.date (“Y-m-d”); 如果(! file_exists ($ this→dir)) { 如果(! (mkdir ($ this→dir),,mkdir ($ this→dirSec))) { $ this→errorMsg='主目录创建失败的; 返回错误; } }elseif (! file_exists ($ this→dirSec)) { 如果(! mkdir ($ this→dirSec)) { $ this→errorMsg='子目录创建失败的; 返回错误; } } 返回true; }//移动文件 私有函数移动文件(){ $ imgName=日期(“YmdHis”)。“_”.mt_rand (10000、99999); 函数($ this→tmpName,美元这→dirSec.self:: DS。imgName美元。”。“.pathinfo ($ this→ext)[“扩展”]); 返回$ this→dirSec.self:: DS。imgName美元。”。“.pathinfo ($ this→ext)(“扩展”); } }代码>
自己写了一个,拿去直接用
文件=带有_file美元[img的];
//新上传(获取的文件信息,上传的文件夹,允许的文件类型,允许的文件大小),
上传=new上传美元(美元)文件,“上传”,[gif, png, jpg, ' jpeg '], 444444444);
上传→美元上传();