PHP有很多值得学习的地方,这里我们主要介绍PHP上传类的解决方案,希望大家通过本文有大的收获。用户可以直接在WEB页面中输入PHP命令代码,因而不需要任何特殊的开发环境。在WEB页面中,所有PHP代码都被放置在“”中。此外,用户还可以选择使用诸如 等的形式。PHP引擎会自动识别并处理页面中所有位于PHP定界符之间的代码。
PHP脚本语言的语法结构与C语言和Perl语言的语法风格非常相似。用户在使用变量前不需要对变量进行声明。使用PHP创建数组的过程也非常简单。PHP还具有基本的面向对象组件功能,可以极大的方便用户有效组织和封装自己编写的代码,下面我们就详细的介绍PHP上传类的问题。
PHP上传类实现代码:
[pre]
<?php
- /** *Fileuploadclass
- *@version1.0.0(ThuAug1801:32:39CST2005) *@authorsanshi
- */ classupLoad
- { /**
- * *@authorsanshi
- *@version1.0.0ThuAug1801:00:18CST2005 *@paramstring$info文件内容
- *@paramstring$fileName生成的文件名 *@returnboolean建立成功返回true
- *@deprecated *建立html文件
- */ functioncreateHtml($info,$fileName)
- { }
- /** *
- *@authorsanshi *@version1.0.0ThuAug1801:03:09CST2005
- *@returnvoid *@deprecated
- *构造函数 */
- functiondownLoad() {}
- /** *
- *@authorsanshi *@version1.0.0ThuAug1801:03:55CST2005
- *@paramstring$fileField在表单中的字段名 *@paramstring$length限制的长度
- *@returnboolean成功返回true *@deprecated
- *功能实现函数 */
- functioninit($fileField,$length='') {
- $files=$_FILES[$fileField]; //用户名需要改动,根据自己的实际情况做改动
- $userName='sanshi'; $fileName=$files['name'];
- $fileType=$files['type']; $fileTemp=$files['tmp_name'];
- $fileSize=empty($length)?($files['size']+10):$length; $fileError=$files['error'];//这块也许php4中没有
- //改为 //if($this->_isType($fileName)||$this->_isBig($length ))
- if(!$this->_isType($fileName)||$this->_isBig($length )||$fileError!=0) {
- //print_r($files); returnfalse;
- }else{ $path=$this->_createDir($userName);//取得路径
- $createFileName=$userName."_".time();//设置当前文件名 $createFileType=$this->getFileType($fileName);//设置文件类别
- return@move_uploaded_file($fileTemp,$path.$createFileName.'.'.$createFileType)?true:false; }
- }
- /** *
- *@authorsanshi *@version1.0.0ThuAug1801:07:43CST2005
- *@paramint$length上传限制的大小 *@returnboolean超过返回true
- *@deprecated *判断是否超过预定大小
- */ function_isBig($length)
- { $bigest='';
- return$big>$bigest?true:false; }
- /** *
- *@authorsanshi *@version1.0.0ThuAug1801:08:55CST2005
- *@paramstring$fileName文件名 *@returnstring$fileType文件后缀
- *@deprecated *取得文件后缀(只取得文件的最后一个后缀名)
- */ functiongetFileType($fileName)
- { returnend(explode('.',$fileName));
- } /**
- * *@authorsanshi
- *@version1.0.0ThuAug1801:10:41CST2005 *@paramstring$fileName文件名
- *@paramboolean$method是否检查多个后缀默认false *@paramint$postFix后缀个数默认为2
- *@returnboolean存在返回true *@deprecated
- *检查文件的后缀是否在类别数组中,类别数组自己设置 *如果$method设置为true则检查文件有几个后缀
- */ function_isType($fileName,$method='false',$postFix=2)
- { //设置类别数组
- $type=array('jpeg', 'gif',
- 'bmp', 'exe');
- $fileName=strtolower($fileName); $fileTypeArray=explode('.',$fileName);
- $fileType=end($fileTypeArray); //判断是否有一个文件有多个后缀
- if($method) {
- if(count($fileTypeArray)>(is_int($postFix)?$postFix:2)) {
- returnfalse; }
- } returnin_array($fileType,$type);
- }
- /** *
- *@authorsanshi *@version1.0.0ThuAug1801:17:19CST2005
- *@paramstring$userName *@returnstring$path
- *@deprecated *建立目录目录格式年/月/日/用户名/
- *权限为755 */
- function_createDir($userName) {
- $root=''; $pathSign=DIRECTORY_SEPARATOR;
- $y=date('Y').$pathSign; $m=date('m').$pathSign;
- $d=date('d').$pathSign; $path=$root.$y.$m.$d.$userName;
- $dirArray=explode($pathSign,$path); $tempDir='';
- foreach($dirArrayas$dir) {
- $tempDir.=$dir.$pathSign; $isFile=file_exists($tempDir);
- clearstatcache(); if(!$isFile&&!is_dir($tempDir))
- { @mkdir($tempDir,0755);
- } }
- return$path.$pathSign; }
- /** *
- *@authorsanshi *@version1.0.0ThuAug1801:19:32CST2005
- *@param string$dirName目录名 *@return boolean可以操作返回true
- *@deprecated *判断操作是否在上传目录
- */ function_isDel($dirName)
- { //注意upLoadDir,一定要与真正使用目录相对应
- $upLoadDir=''; $upLoadDir=preg_replace('/\\//','\/',$upLoadDir);
- $format="/^{$upLoadDir}/"; returnpreg_match($format,$dirName);
- } /**
- * *@authorsanshi
- *@version1.0.0ThuAug1801:25:58CST2005 *@paramstring$fileName文件名
- *@returnboolean删除文件成功返回true *@deprecated
- *删除文件 */
- functiondelFile($fileName) {
- $cur_dir=dirname(trim($fileName)); if($this->_isDel($cur_dir))
- { return@unlink($fileName)?true:false;
- }else{ returnfalse;
- } }
- /** *
- *@authorsanshi *@version1.0.0ThuAug1801:27:43CST2005
- *@paramstring$dieName目录名 *@returnboolean删除成功返回true
- *@deprecated *删除目录目录下如果有文件不能删除
- */ functiondelDir($dirName)
- { if($this->_isDel($dirName)&&is_dir($dirName))
- { return@rmdir($dirName)?true:false;
- }else{ returnfalse;
- } }
- }
- ?> <?php
- //使用 /*
- include'upLoad.class.php'; $up=newupLoad();
- if($up->init("file")) {
- echo'success'; }else{
- echo'failure'; }
- */
[/pre]
|