1. Trang chủ
  2. » Công Nghệ Thông Tin

Chuyển nội dung file sang XML

6 2,6K 2
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 6
Dung lượng 13,24 KB

Nội dung

Chuyển nội dung file sang XML

Chuyển nội dung file sang XML class_file2xml.phpview plainprint? 1. <? 2. /*=============================== 3. - Class: File2XML 4. - Author: Huynh Hai Huynh 5. - Email: Huynhhaihuynh@gmail.com 6. - Website: http://www.php.net.vn 7. ===============================*/ 8. 9. class file2xml 10. { 11. 12. 13. var $folder_read; 14. 15. var $folder_write; 16. 17. var $xml_write; 18. 19. var $ext_read = array(); 20. 21. var $file_in = array(); 22. 23. var $time_limit; 24. 25. var $size_limit; 26. 27. function listfile() 28. { 29. 30. $file_in = array(); 31. 32. if ($handle = @opendir($this->folder_read)) 33. { 34. while (false !== ($file = readdir($handle))) 35. { 36. if ($file != "." && $file != " ") 37. { 38. 39. if (is_file($this->folder_read."/".$file)) 40. { 41. 42. 43. $mfile = explode(".",$file); 44. 45. 46. $f_size = filesize($this->folder_read."/".$file); 47. 48. $ext = $mfile[count($mfile)-1]; 49. 50. 51. 52. if (count($mfile)==1 and in_array("NULL",$this->ext_read) and ($f_size <= $this->size_limit)) 53. { 54. $file_in[] .= $file; 55. } 56. elseif (in_array(strtolower($ext),$this->ext_read) and ($f_size <= $this->size_limit)) 57. { 58. $file_in[] .= $file; 59. } 60. 61. } 62. 63. } 64. } 65. closedir($handle); 66. 67. $this->file_in = $file_in; 68. 69. $this->dumpxml(); 70. 71. } 72. else die("Can not read this folder: ". $this->folder_read ); 73. 74. } 75. 76. function dumpxml() 77. { 78. 79. 80. 81. 82. if (count($this->file_in)>0) 83. { 84. 85. // SET time excute 86. ini_set("max_execution_time",$this->time_limit); 87. 88. // date 89. 90. $date = date("H:j d-m-Y"); 91. 92. //repare file forwrite 93. $handle_w = fopen($this->folder_save."/".$this->xml_write, "w+"); 94. 95. fclose($handle_w); 96. 97. echo "================================<br>"; 98. echo "<b>File2XML v1.0</b><br>"; 99. echo " Author: <b>Huynh Hai Huynh</b><br>"; 100. echo " Email: <b>huynhhaihuynh@gmail.com</b><br>"; 101. echo " Creating file: <b>".$this->xml_write."</b><br>"; 102. echo " Save at: <b>".$this->folder_save."</b><br>"; 103. echo " Date: <b>".$date."</b><br>"; 104. echo "================================<br>"; 105. // Create XML struc 106. $dom = new DOMDocument('1.0', 'iso-8859-1'); 107. 108. $element = $dom->appendChild(new DOMElement('information')); 109. 110. $element_auth = new DOMElement('author', 'FILE to XML coded by LM - huynhhaihuynh@gmail'); 111. 112. $element_version = new DOMElement('version', 'File2XML v1.0'); 113. 114. $element_date = new DOMElement('date_created', $date); 115. 116. $element_file = new DOMElement('file_convert', count($this->file_in)); 117. 118. $element_conv = $element->appendChild(new DOMElement('main')); 119. 120. $is=0; 121. 122. $total = array(); 123. 124. foreach ($this->file_in as $fl) 125. 126. { 127. 128. $is++; 129. 130. $fl_full = $this->folder_read.$fl; 131. 132. $handle = fopen($fl_full , "r"); 133. 134. while (!feof($handle)) 135. { 136. $buffer = fgets($handle, 4096); 137. 138. $total[$is].=$buffer; 139. } 140. 141. fclose($handle); 142. 143. 144. 145. $element_conv_file_details = $element_conv->appendChild(new DOMElement('file')); 146. 147. $element_conv_file_name = new DOMElement('file_name', $fl); 148. 149. $element_conv_file_size = new DOMElement('file_size', @filesize($fl_full)." bytes"); 150. 151. $element_conv_file_data = new DOMElement('file_data', base64_encode($total[$is])); 152. 153. $element_conv_file_details->appendChild($element_conv_file_name); 154. 155. $element_conv_file_details->appendChild( $element_conv_file_size); 156. 157. $element_conv_file_details->appendChild( $element_conv_file_data); 158. 159. 160. echo "Writing file: $fl<br>"; 161. 162. } 163. 164. 165. $element->appendChild($element_auth); 166. 167. $element->appendChild($element_version); 168. 169. $element->appendChild($element_date); 170. 171. $element->appendChild($element_file); 172. 173. $element->appendChild($element_conv); 174. 175. $con_save = $dom->save($this->folder_save."/".$this->xml_write); 176. 177. echo "================================<br>"; 178. 179. echo "DONE! Your files are writen successful: ".filesize($this->folder_save."/".$this->xml_write)." bytes"; 180. 181. // End 182. 183. } 184. else die("No list file found"); 185. 186. } 187. 188. 189. 190. 191. } 192. 193. ?> <? /*=============================== - Class: File2XML - Author: Huynh Hai Huynh - Email: Huynhhaihuynh@gmail.com - Website: http://www.php.net.vn ===============================*/ class file2xml { var $folder_read; var $folder_write; var $xml_write; var $ext_read = array(); var $file_in = array(); var $time_limit; var $size_limit; function listfile() { $file_in = array(); if ($handle = @opendir($this->folder_read)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != " ") { if (is_file($this->folder_read."/".$file)) { $mfile = explode(".",$file); $f_size = filesize($this->folder_read."/".$file); $ext = $mfile[count($mfile)-1]; if (count($mfile)==1 and in_array("NULL",$this->ext_read) and ($f_size <= $this->size_limit)) { $file_in[] .= $file; } elseif (in_array(strtolower($ext),$this->ext_read) and ($f_size <= $this->size_limit)) { $file_in[] .= $file; } } } } closedir($handle); $this->file_in = $file_in; $this->dumpxml(); } else die("Can not read this folder: ". $this->folder_read ); } function dumpxml() { if (count($this->file_in)>0) { // SET time excute ini_set("max_execution_time",$this->time_limit); // date $date = date("H:j d-m-Y"); //repare file forwrite $handle_w = fopen($this->folder_save."/".$this->xml_write, "w+"); fclose($handle_w); echo "================================<br>"; echo "<b>File2XML v1.0</b><br>"; echo " Author: <b>Huynh Hai Huynh</b><br>"; echo " Email: <b>huynhhaihuynh@gmail.com</b><br>"; echo " Creating file: <b>".$this->xml_write."</b><br>"; echo " Save at: <b>".$this->folder_save."</b><br>"; echo " Date: <b>".$date."</b><br>"; echo "================================<br>"; // Create XML struc $dom = new DOMDocument('1.0', 'iso-8859-1'); $element = $dom->appendChild(new DOMElement('information')); $element_auth = new DOMElement('author', 'FILE to XML coded by LM - huynhhaihuynh@gmail'); $element_version = new DOMElement('version', 'File2XML v1.0'); $element_date = new DOMElement('date_created', $date); $element_file = new DOMElement('file_convert', count($this->file_in)); $element_conv = $element->appendChild(new DOMElement('main')); $is=0; $total = array(); foreach ($this->file_in as $fl) { $is++; $fl_full = $this->folder_read.$fl; $handle = fopen($fl_full , "r"); while (!feof($handle)) { $buffer = fgets($handle, 4096); $total[$is].=$buffer; } fclose($handle); $element_conv_file_details = $element_conv->appendChild(new DOMElement('file')); $element_conv_file_name = new DOMElement('file_name', $fl); $element_conv_file_size = new DOMElement('file_size', @filesize($fl_full)." bytes"); $element_conv_file_data = new DOMElement('file_data', base64_encode($total[$is])); $element_conv_file_details->appendChild($element_conv_file_name); $element_conv_file_details->appendChild( $element_conv_file_size); $element_conv_file_details->appendChild( $element_conv_file_data); echo "Writing file: $fl<br>"; } $element->appendChild($element_auth); $element->appendChild($element_version); $element->appendChild($element_date); $element->appendChild($element_file); $element->appendChild($element_conv); $con_save = $dom->save($this->folder_save."/".$this->xml_write); echo "================================<br>"; echo "DONE! Your files are writen successful: ".filesize($this->folder_save."/".$this->xml_write)." bytes"; // End } else die("No list file found"); } } ?>usage.phpview plainprint? 1. <? 2. /*=============================== 3. - Class: File2XML 4. - Author: Huynh Hai Huynh 5. - Email: Huynhhaihuynh@gmail.com 6. - Website: http://www.php.net.vn 7. - Usage.php 8. ===============================*/ 9. require_once("class_file2xml.php"); 10. $k = new file2xml; 11. 12. $k->folder_read = "./"; // Folder ctontains file need encode 13. 14. $k->folder_save = "./"; // Folder save XMLoutput file 15. 16. $k->xml_write = "dump_file.xml"; // File name XML 17. 18. $k->time_limit = "600"; // Time execution 19. 20. $k->size_limit = "500000000"; // LIMIT bytes for per file 21. 22. $k->ext_read = array("ico"); // Null is file without extension 23. /* 24. array("doc","mpg","txt","php","gif"); 25. EG: file no exteions key in array is NULL 26. array("NULL","mpg","txt","php","gif"); 27. 28. */ 29. 30. echo $k->listfile(); 31. 32. ?> <? /*=============================== - Class: File2XML - Author: Huynh Hai Huynh - Email: Huynhhaihuynh@gmail.com - Website: http://www.php.net.vn - Usage.php ===============================*/ require_once("class_file2xml.php"); $k = new file2xml; $k->folder_read = "./"; // Folder ctontains file need encode $k->folder_save = "./"; // Folder save XMLoutput file $k->xml_write = "dump_file.xml"; // File name XML $k->time_limit = "600"; // Time execution $k->size_limit = "500000000"; // LIMIT bytes for per file $k->ext_read = array("ico"); // Null is file without extension /* array("doc","mpg","txt","php","gif"); EG: file no exteions key in array is NULL array("NULL","mpg","txt","php","gif"); */ echo $k->listfile(); ?>http://www.phpclasses.org/browse/package/3363.html . Chuyển nội dung file sang XML class _file2 xml. phpview plainprint? 1. <? 2. /*=============================== 3. - Class: File2 XML . "./"; // Folder save XMLoutput file 15. 16. $k-> ;xml_ write = "dump _file. xml& quot;; // File name XML 17. 18. $k->time_limit

Ngày đăng: 07/09/2012, 11:40

TỪ KHÓA LIÊN QUAN

w