FileUpload.php
上传用户:wlfwy2004
上传日期:2016-12-12
资源大小:33978k
文件大小:8k
源码类别:

Jsp/Servlet

开发平台:

Java

  1. <?php 
  2. /*
  3.  * FCKeditor - The text editor for internet
  4.  * Copyright (C) 2003-2005 Frederico Caldeira Knabben
  5.  * 
  6.  * Licensed under the terms of the GNU Lesser General Public License:
  7.  *  http://www.opensource.org/licenses/lgpl-license.php
  8.  * 
  9.  * For further information visit:
  10.  *  http://www.fckeditor.net/
  11.  * 
  12.  * File Name: FileUpload.php
  13.  *  Implements the FileUpload command,
  14.  *  Checks the file uploaded is allowed, 
  15.  *  then moves it to the user data area. 
  16.  * 
  17.  * File Authors:
  18.  *  Grant French (grant@mcpuk.net)
  19.  */
  20. class FileUpload {
  21. var $fckphp_config;
  22. var $type;
  23. var $cwd;
  24. var $actual_cwd;
  25. var $newfolder;
  26. function FileUpload($fckphp_config,$type,$cwd) {
  27. $this->fckphp_config=$fckphp_config;
  28. $this->type=$type;
  29. $this->raw_cwd=$cwd;
  30. $this->actual_cwd=str_replace("//","/",($this->fckphp_config['UserFilesPath']."/$type/".$this->raw_cwd));
  31. $this->real_cwd=str_replace("//","/",($this->fckphp_config['basedir']."/".$this->actual_cwd));
  32. }
  33. function cleanFilename($filename) {
  34. $n_filename="";
  35. //Check that it only contains valid characters
  36. for($i=0;$i<strlen($filename);$i++) if (in_array(substr($filename,$i,1),$this->fckphp_config['FileNameAllowedChars'])) $n_filename.=substr($filename,$i,1);
  37. //If it got this far all is ok
  38. return $n_filename;
  39. }
  40. function run() {
  41. //If using CGI Upload script, get file info and insert into $_FILE array
  42. if  (
  43. (sizeof($_FILES)==0) && 
  44. isset($_GET['file']) && 
  45. isset($_GET['file']['NewFile']) && 
  46. is_array($_GET['file']['NewFile'])
  47. ) {
  48. if (isset($_GET['file']['NewFile']['name'])&&$_GET['file']['NewFile']['size']&&$_GET['file']['NewFile']['tmp_name']) {
  49. $_FILES['NewFile']['name']=basename(str_replace("\","/",$_GET['file']['NewFile']['name']));
  50. $_FILES['NewFile']['size']=$_GET['file']['NewFile']['size'];
  51. $_FILES['NewFile']['tmp_name']=$_GET['file']['NewFile']['tmp_name'];
  52. } else {
  53. $disp="202,'Incomplete file information from upload CGI'";
  54. }
  55. }
  56. //  if (isset($_FILES['NewFile'])&&isset($_FILES['NewFile']['name'])&&($_FILES['NewFile']['name']!=""))
  57. //  $_FILES['NewFile']['name']=$_FILES['NewFile']['name']; //$this->cleanFilename($_FILES['NewFile']['name']);
  58. $typeconfig=$this->fckphp_config['ResourceAreas'][$this->type];
  59. header ("content-type: text/html");
  60. if (sizeof($_FILES)>0) {
  61. if (array_key_exists("NewFile",$_FILES)) {
  62. if ($_FILES['NewFile']['size']<($typeconfig['MaxSize']*1024)) {
  63. $filename=basename(str_replace("\","/",$_FILES['NewFile']['name']));
  64. $lastdot=strrpos($filename,".");
  65. if ($lastdot!==false) {
  66. $ext=substr($filename,($lastdot+1));
  67. $filename=substr($filename,0,$lastdot);
  68. if (in_array(strtolower($ext),$typeconfig['AllowedExtensions'])) {
  69. $test=0;
  70. $dirSizes=array();
  71. $globalSize=0;
  72. $failSizeCheck=false;
  73. if ($this->fckphp_config['DiskQuota']['Global']!=-1) {
  74. foreach ($this->fckphp_config['ResourceTypes'] as $resType) {
  75. $dirSizes[$resType]=
  76. $this->getDirSize(
  77. $this->fckphp_config['basedir']."/".$this->fckphp_config['UserFilesPath']."/$resType");
  78. if ($dirSizes[$resType]===false) {
  79. //Failed to stat a directory, fall out
  80. $failSizeCheck=true;
  81. $msg="\nUnable to determine the size of a folder.";
  82. break;
  83. }
  84. $globalSize+=$dirSizes[$resType];
  85. }
  86. $globalSize+=$_FILES['NewFile']['size'];
  87. if (!$failSizeCheck) {
  88. if ($globalSize>($this->fckphp_config['DiskQuota']['Global']*1048576)) {
  89. $failSizeCheck=true;
  90. $msg="\nYou are over the global disk quota.";
  91. }
  92. }
  93. }
  94. if (($typeconfig['DiskQuota']!=-1)&&(!$failSizeCheck)) {
  95. if ($this->fckphp_config['DiskQuota']['Global']==-1) {
  96. $dirSizes[$this->type]=
  97. $this->getDirSize(
  98. $this->fckphp_config['basedir']."/".$this->fckphp_config['UserFilesPath']."/".$this->type);
  99. }
  100. if (($dirSizes[$this->type]+$_FILES['NewFile']['size'])>
  101. ($typeconfig['DiskQuota']*1048576)) {
  102. $failSizeCheck=true;
  103. $msg="\nYou are over the disk quota for this resource type.";
  104. }
  105. }
  106. if ((($this->fckphp_config['DiskQuota']['Global']!=-1)||($typeconfig['DiskQuota']!=-1))&&$failSizeCheck) {
  107. //Disk Quota over
  108. $disp="202,'Over disk quota, ".$msg."'";
  109. } else {
  110. if (file_exists($this->real_cwd."/$filename.$ext")) {
  111. $taskDone=false;
  112. //File already exists, try renaming
  113. //If there are more than 200 files with
  114. // the same name giveup
  115. for ($i=1;(($i<200)&&($taskDone==false));$i++) {
  116. if (!file_exists($this->real_cwd."/$filename($i).$ext")) {
  117. if (is_uploaded_file($_FILES['NewFile']['tmp_name'])) {
  118. if 
  119. (move_uploaded_file($_FILES['NewFile']['tmp_name'],($this->real_cwd."/$filename($i).$ext"))) {
  120. chmod(($this->real_cwd."/$filename($i).$ext"),0777);
  121. $disp="201,'..$filename($i).$ext'";
  122. } else {
  123. $disp="202,'Failed to upload file, internal error.'";
  124. }
  125. } else {
  126. if 
  127. (rename($_FILES['NewFile']['tmp_name'],($this->real_cwd."/$filename($i).$ext"))) {
  128. chmod(($this->real_cwd."/$filename($i).$ext"),0777);
  129. $disp="201,'$filename($i).$ext'";
  130. } else {
  131. $disp="202,'Failed to upload file, internal error.'";
  132. }
  133. }
  134. $taskDone=true;
  135. }
  136. }
  137. if ($taskDone==false) {
  138. $disp="202,'Failed to upload file, internal error..'";
  139. }
  140. } else {
  141. //Upload file
  142. if (is_uploaded_file($_FILES['NewFile']['tmp_name'])) {
  143. if (move_uploaded_file($_FILES['NewFile']['tmp_name'],($this->real_cwd."/$filename.$ext"))) {
  144. chmod(($this->real_cwd."/$filename.$ext"),0777);
  145. $disp="0";
  146. } else {
  147. $disp="202,'Failed to upload file, internal error...'";
  148. }
  149. } else {
  150. if (rename($_FILES['NewFile']['tmp_name'],($this->real_cwd."/$filename.$ext"))) {
  151. chmod(($this->real_cwd."/$filename.$ext"),0777);
  152. $disp="0";
  153. } else {
  154. $disp="202,'Failed to upload file, internal error...'";
  155. }
  156. }
  157. }
  158. }
  159. } else {
  160. //Disallowed file extension
  161. $disp="202,'Disallowed file type.'";
  162. }
  163. } else {
  164. //No file extension to check
  165. $disp="202,'Unable to determine file type of file'";
  166. }
  167. } else {
  168. //Too big
  169. $disp="202,'This file exceeds the maximum upload size.'";
  170. }
  171. } else {
  172. //No file uploaded with field name NewFile
  173. $disp="202,'Unable to find uploaded file.'";
  174. }
  175. } else {
  176. //No files uploaded
  177. //Should really send something back saying
  178. //invalid file, but this breaks the filemanager 
  179. //with firefox, so for now we'll just exit
  180. exit(0);
  181. //$disp="202";
  182. }
  183. ?>
  184. <html>
  185. <head>
  186. <title>Upload Complete</title>
  187. </head>
  188. <body>
  189. <script type="text/javascript">
  190. window.parent.frames['frmUpload'].OnUploadCompleted(<?php echo $disp; ?>) ;
  191. </script>
  192. </body>
  193. </html>
  194. <?php
  195. }
  196. function getDirSize($dir) {
  197. $dirSize=0;
  198. if ($dh=@opendir($dir)) {
  199. while ($file=@readdir($dh)) {
  200. if (($file!=".")&&($file!="..")) {
  201. if (is_dir($dir."/".$file)) {
  202. $tmp_dirSize=$this->getDirSize($dir."/".$file);
  203. if ($tmp_dirSize!==false) $dirSize+=$tmp_dirSize;
  204. } else {
  205. $dirSize+=filesize($dir."/".$file);
  206. }
  207. }
  208. }
  209. @closedir($dh);
  210. } else {
  211. return false;
  212. }
  213. return $dirSize;
  214. }
  215. }
  216. ?>