+ -
当前位置:首页 → 问答吧 → 五分钟PHP上传类实现

五分钟PHP上传类实现

时间:2011-03-24

来源:互联网


PHP有很多值得学习的地方,这里我们主要介绍PHP上传类的解决方案,希望大家通过本文有大的收获。用户可以直接在WEB页面中输入PHP命令代码,因而不需要任何特殊的开发环境。在WEB页面中,所有PHP代码都被放置在“”中。此外,用户还可以选择使用诸如 等的形式。PHP引擎会自动识别并处理页面中所有位于PHP定界符之间的代码。


PHP脚本语言的语法结构与C语言和Perl语言的语法风格非常相似。用户在使用变量前不需要对变量进行声明。使用PHP创建数组的过程也非常简单。PHP还具有基本的面向对象组件功能,可以极大的方便用户有效组织和封装自己编写的代码,下面我们就详细的介绍PHP上传类的问题。
 
PHP上传类实现代码:

[pre]
    <?php
  1. /**  *Fileuploadclass  
  2. *@version1.0.0(ThuAug1801:32:39CST2005)  *@authorsanshi  
  3. */  classupLoad  
  4. {  /**  
  5. *  *@authorsanshi  
  6. *@version1.0.0ThuAug1801:00:18CST2005  *@paramstring$info文件内容  
  7. *@paramstring$fileName生成的文件名  *@returnboolean建立成功返回true  
  8. *@deprecated  *建立html文件  
  9. */  functioncreateHtml($info,$fileName)  
  10. {  }  
  11. /**  *  
  12. *@authorsanshi  *@version1.0.0ThuAug1801:03:09CST2005  
  13. *@returnvoid  *@deprecated  
  14. *构造函数  */  
  15. functiondownLoad()  {}  
  16. /**  *  
  17. *@authorsanshi  *@version1.0.0ThuAug1801:03:55CST2005  
  18. *@paramstring$fileField在表单中的字段名  *@paramstring$length限制的长度  
  19. *@returnboolean成功返回true  *@deprecated  
  20. *功能实现函数  */  
  21. functioninit($fileField,$length='')  {  
  22. $files=$_FILES[$fileField];  //用户名需要改动,根据自己的实际情况做改动  
  23. $userName='sanshi';  $fileName=$files['name'];  
  24. $fileType=$files['type'];  $fileTemp=$files['tmp_name'];  
  25. $fileSize=empty($length)?($files['size']+10):$length;  $fileError=$files['error'];//这块也许php4中没有  
  26. //改为  //if($this->_isType($fileName)||$this->_isBig($length ))  
  27. if(!$this->_isType($fileName)||$this->_isBig($length )||$fileError!=0)  {  
  28. //print_r($files);  returnfalse;  
  29. }else{  $path=$this->_createDir($userName);//取得路径  
  30. $createFileName=$userName."_".time();//设置当前文件名  $createFileType=$this->getFileType($fileName);//设置文件类别  
  31. return@move_uploaded_file($fileTemp,$path.$createFileName.'.'.$createFileType)?true:false;  }  
  32. }  
  33. /**  *  
  34. *@authorsanshi  *@version1.0.0ThuAug1801:07:43CST2005  
  35. *@paramint$length上传限制的大小  *@returnboolean超过返回true  
  36. *@deprecated  *判断是否超过预定大小  
  37. */  function_isBig($length)  
  38. {  $bigest='';  
  39. return$big>$bigest?true:false;  }  
  40. /**  *  
  41. *@authorsanshi  *@version1.0.0ThuAug1801:08:55CST2005  
  42. *@paramstring$fileName文件名  *@returnstring$fileType文件后缀  
  43. *@deprecated  *取得文件后缀(只取得文件的最后一个后缀名)  
  44. */  functiongetFileType($fileName)  
  45. {  returnend(explode('.',$fileName));  
  46. }  /**  
  47. *  *@authorsanshi  
  48. *@version1.0.0ThuAug1801:10:41CST2005  *@paramstring$fileName文件名  
  49. *@paramboolean$method是否检查多个后缀默认false  *@paramint$postFix后缀个数默认为2  
  50. *@returnboolean存在返回true  *@deprecated  
  51. *检查文件的后缀是否在类别数组中,类别数组自己设置  *如果$method设置为true则检查文件有几个后缀  
  52. */  function_isType($fileName,$method='false',$postFix=2)  
  53. {  //设置类别数组  
  54. $type=array('jpeg',  'gif',  
  55. 'bmp',  'exe');  
  56. $fileName=strtolower($fileName);  $fileTypeArray=explode('.',$fileName);  
  57. $fileType=end($fileTypeArray);  //判断是否有一个文件有多个后缀  
  58. if($method)  {  
  59. if(count($fileTypeArray)>(is_int($postFix)?$postFix:2))  {  
  60. returnfalse;  }  
  61. }  returnin_array($fileType,$type);  
  62. }  
  63. /**  *  
  64. *@authorsanshi  *@version1.0.0ThuAug1801:17:19CST2005  
  65. *@paramstring$userName  *@returnstring$path  
  66. *@deprecated  *建立目录目录格式年/月/日/用户名/  
  67. *权限为755  */  
  68. function_createDir($userName)  {  
  69. $root='';  $pathSign=DIRECTORY_SEPARATOR;  
  70. $y=date('Y').$pathSign;  $m=date('m').$pathSign;  
  71. $d=date('d').$pathSign;  $path=$root.$y.$m.$d.$userName;  
  72. $dirArray=explode($pathSign,$path);  $tempDir='';  
  73. foreach($dirArrayas$dir)  {  
  74. $tempDir.=$dir.$pathSign;  $isFile=file_exists($tempDir);  
  75. clearstatcache();  if(!$isFile&&!is_dir($tempDir))  
  76. {  @mkdir($tempDir,0755);  
  77. }  }  
  78. return$path.$pathSign;  }  
  79. /**  *  
  80. *@authorsanshi  *@version1.0.0ThuAug1801:19:32CST2005  
  81. *@param string$dirName目录名  *@return boolean可以操作返回true  
  82. *@deprecated  *判断操作是否在上传目录  
  83. */  function_isDel($dirName)  
  84. {  //注意upLoadDir,一定要与真正使用目录相对应  
  85. $upLoadDir='';  $upLoadDir=preg_replace('/\\//','\/',$upLoadDir);  
  86. $format="/^{$upLoadDir}/";  returnpreg_match($format,$dirName);  
  87. }  /**  
  88. *  *@authorsanshi  
  89. *@version1.0.0ThuAug1801:25:58CST2005  *@paramstring$fileName文件名  
  90. *@returnboolean删除文件成功返回true  *@deprecated  
  91. *删除文件  */  
  92. functiondelFile($fileName)  {  
  93. $cur_dir=dirname(trim($fileName));  if($this->_isDel($cur_dir))  
  94. {  return@unlink($fileName)?true:false;  
  95. }else{  returnfalse;  
  96. }  }  
  97. /**  *  
  98. *@authorsanshi  *@version1.0.0ThuAug1801:27:43CST2005  
  99. *@paramstring$dieName目录名  *@returnboolean删除成功返回true  
  100. *@deprecated  *删除目录目录下如果有文件不能删除  
  101. */  functiondelDir($dirName)  
  102. {  if($this->_isDel($dirName)&&is_dir($dirName))  
  103. {  return@rmdir($dirName)?true:false;  
  104. }else{  returnfalse;  
  105. }  }  
  106. }  
  107. ?> <?php
  108. //使用  /*  
  109. include'upLoad.class.php';  $up=newupLoad();  
  110. if($up->init("file"))  {  
  111. echo'success';  }else{  
  112. echo'failure';  }  
  113. */  
[/pre]

作者: halennet   发布时间: 2011-03-24

新人进来顶一下

作者: benetwb   发布时间: 2011-03-24