file_manager_json.asp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <%@ CODEPAGE=65001 %>
  2. <% Option Explicit %>
  3. <% Response.CodePage=65001 %>
  4. <% Response.Charset="UTF-8" %>
  5. <!--#include file="JSON_2.0.4.asp"-->
  6. <%
  7. ' KindEditor ASP
  8. '
  9. ' 本ASP程序是演示程序,建议不要直接在实际项目中使用。
  10. ' 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
  11. '
  12. Dim aspUrl, rootPath, rootUrl, fileTypes
  13. Dim currentPath, currentUrl, currentDirPath, moveupDirPath
  14. Dim path, order, dirName, fso, folder, dir, file, result
  15. Dim fileExt, dirCount, fileCount, orderIndex, i, j
  16. Dim dirList(), fileList(), isDir, hasFile, filesize, isPhoto, filetype, filename, datetime
  17. aspUrl = Request.ServerVariables("SCRIPT_NAME")
  18. aspUrl = left(aspUrl, InStrRev(aspUrl, "/"))
  19. '根目录路径,可以指定绝对路径,比如 /var/www/attached/
  20. rootPath = "../attached/"
  21. '根目录URL,可以指定绝对路径,比如 http://www.yoursite.com/attached/
  22. rootUrl = aspUrl & "../attached/"
  23. '图片扩展名
  24. fileTypes = "gif,jpg,jpeg,png,bmp"
  25. currentPath = ""
  26. currentUrl = ""
  27. currentDirPath = ""
  28. moveupDirPath = ""
  29. Set fso = Server.CreateObject("Scripting.FileSystemObject")
  30. '目录名
  31. dirName = Request.QueryString("dir")
  32. If Not isEmpty(dirName) Then
  33. If instr(lcase("image,flash,media,file"), dirName) < 1 Then
  34. Response.Write "Invalid Directory name."
  35. Response.End
  36. End If
  37. rootPath = rootPath & dirName & "/"
  38. rootUrl = rootUrl & dirName & "/"
  39. If Not fso.FolderExists(Server.mappath(rootPath)) Then
  40. fso.CreateFolder(Server.mappath(rootPath))
  41. End If
  42. End If
  43. '根据path参数,设置各路径和URL
  44. path = Request.QueryString("path")
  45. If path = "" Then
  46. currentPath = Server.MapPath(rootPath) & "\"
  47. currentUrl = rootUrl
  48. currentDirPath = ""
  49. moveupDirPath = ""
  50. Else
  51. currentPath = Server.MapPath(rootPath & path) & "\"
  52. currentUrl = rootUrl + path
  53. currentDirPath = path
  54. moveupDirPath = RegexReplace(currentDirPath, "(.*?)[^\/]+\/$", "$1")
  55. End If
  56. Set folder = fso.GetFolder(currentPath)
  57. '排序形式,name or size or type
  58. order = lcase(Request.QueryString("order"))
  59. Select Case order
  60. Case "type" orderIndex = 4
  61. Case "size" orderIndex = 2
  62. Case Else orderIndex = 5
  63. End Select
  64. '不允许使用..移动到上一级目录
  65. If RegexIsMatch(path, "\.\.") Then
  66. Response.Write "Access is not allowed."
  67. Response.End
  68. End If
  69. '最后一个字符不是/
  70. If path <> "" And Not RegexIsMatch(path, "\/$") Then
  71. Response.Write "Parameter is not allowed."
  72. Response.End
  73. End If
  74. '目录不存在或不是目录
  75. If Not DirectoryExists(currentPath) Then
  76. Response.Write "Directory does not exist."
  77. Response.End
  78. End If
  79. Set result = jsObject()
  80. '相对于根目录的上一级目录
  81. result("moveup_dir_path") = moveupDirPath
  82. '相对于根目录的当前目录
  83. result("current_dir_path") = currentDirPath
  84. '当前目录的URL
  85. result("current_url") = currentUrl
  86. '文件数
  87. dirCount = folder.SubFolders.count
  88. fileCount = folder.Files.count
  89. result("total_count") = dirCount + fileCount
  90. ReDim dirList(dirCount)
  91. i = 0
  92. For Each dir in folder.SubFolders
  93. isDir = True
  94. hasFile = (dir.Files.count > 0)
  95. filesize = 0
  96. isPhoto = False
  97. filetype = ""
  98. filename = dir.name
  99. datetime = FormatDate(dir.DateLastModified)
  100. dirList(i) = Array(isDir, hasFile, filesize, isPhoto, filetype, filename, datetime)
  101. i = i + 1
  102. Next
  103. ReDim fileList(fileCount)
  104. i = 0
  105. For Each file in folder.Files
  106. fileExt = lcase(mid(file.name, InStrRev(file.name, ".") + 1))
  107. isDir = False
  108. hasFile = False
  109. filesize = file.size
  110. isPhoto = (instr(lcase(fileTypes), fileExt) > 0)
  111. filetype = fileExt
  112. filename = file.name
  113. datetime = FormatDate(file.DateLastModified)
  114. fileList(i) = Array(isDir, hasFile, filesize, isPhoto, filetype, filename, datetime)
  115. i = i + 1
  116. Next
  117. '排序
  118. Dim minidx, temp
  119. For i = 0 To dirCount - 2
  120. minidx = i
  121. For j = i + 1 To dirCount - 1
  122. If (dirList(minidx)(5) > dirList(j)(5)) Then
  123. minidx = j
  124. End If
  125. Next
  126. If minidx <> i Then
  127. temp = dirList(minidx)
  128. dirList(minidx) = dirList(i)
  129. dirList(i) = temp
  130. End If
  131. Next
  132. For i = 0 To fileCount - 2
  133. minidx = i
  134. For j = i + 1 To fileCount - 1
  135. If (fileList(minidx)(orderIndex) > fileList(j)(orderIndex)) Then
  136. minidx = j
  137. End If
  138. Next
  139. If minidx <> i Then
  140. temp = fileList(minidx)
  141. fileList(minidx) = fileList(i)
  142. fileList(i) = temp
  143. End If
  144. Next
  145. Set result("file_list") = jsArray()
  146. For i = 0 To dirCount - 1
  147. Set result("file_list")(Null) = jsObject()
  148. result("file_list")(Null)("is_dir") = dirList(i)(0)
  149. result("file_list")(Null)("has_file") = dirList(i)(1)
  150. result("file_list")(Null)("filesize") = dirList(i)(2)
  151. result("file_list")(Null)("is_photo") = dirList(i)(3)
  152. result("file_list")(Null)("filetype") = dirList(i)(4)
  153. result("file_list")(Null)("filename") = dirList(i)(5)
  154. result("file_list")(Null)("datetime") = dirList(i)(6)
  155. Next
  156. For i = 0 To fileCount - 1
  157. Set result("file_list")(Null) = jsObject()
  158. result("file_list")(Null)("is_dir") = fileList(i)(0)
  159. result("file_list")(Null)("has_file") = fileList(i)(1)
  160. result("file_list")(Null)("filesize") = fileList(i)(2)
  161. result("file_list")(Null)("is_photo") = fileList(i)(3)
  162. result("file_list")(Null)("filetype") = fileList(i)(4)
  163. result("file_list")(Null)("filename") = fileList(i)(5)
  164. result("file_list")(Null)("datetime") = fileList(i)(6)
  165. Next
  166. '输出JSON字符串
  167. Response.AddHeader "Content-Type", "text/html; charset=UTF-8"
  168. result.Flush
  169. Response.End
  170. '自定义函数
  171. Function DirectoryExists(dirPath)
  172. Dim fso
  173. Set fso = Server.CreateObject("Scripting.FileSystemObject")
  174. DirectoryExists = fso.FolderExists(dirPath)
  175. End Function
  176. Function RegexIsMatch(subject, pattern)
  177. Dim reg
  178. Set reg = New RegExp
  179. reg.Global = True
  180. reg.MultiLine = True
  181. reg.Pattern = pattern
  182. RegexIsMatch = reg.Test(subject)
  183. End Function
  184. Function RegexReplace(subject, pattern, replacement)
  185. Dim reg
  186. Set reg = New RegExp
  187. reg.Global = True
  188. reg.MultiLine = True
  189. reg.Pattern = pattern
  190. RegexReplace = reg.Replace(subject, replacement)
  191. End Function
  192. Public Function FormatDate(datetime)
  193. Dim y, m, d, h, i, s
  194. y = CStr(Year(datetime))
  195. m = CStr(Month(datetime))
  196. If Len(m) = 1 Then m = "0" & m
  197. d = CStr(Day(datetime))
  198. If Len(d) = 1 Then d = "0" & d
  199. h = CStr(Hour(datetime))
  200. If Len(h) = 1 Then h = "0" & h
  201. i = CStr(Minute(datetime))
  202. If Len(i) = 1 Then i = "0" & i
  203. s = CStr(Second(datetime))
  204. If Len(s) = 1 Then s = "0" & s
  205. FormatDate = y & "-" & m & "-" & d & " " & h & ":" & i & ":" & s
  206. End Function
  207. %>