Guest User

Untitled

a guest
Oct 15th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.  
  5. </style>
  6. <meta charset="UTF-8">
  7. <title> Generator</title>
  8.  
  9.  
  10. </head>
  11. <
  12.  
  13. body>
  14.  
  15.  
  16. </span></span>
  17.  
  18.  
  19.  
  20. <form action="" method="post" enctype="multipart/form-data" onsubmit="return validate(this)">
  21. <input type="hidden" name="<?php echo ini_get('session.upload_progress.prefix').ini_get('session.upload_progress.name'); ?>" value="123" />
  22. <labeL>
  23. <span>Choose CSV file:</span>
  24. </span></span>
  25. <input type="file" accept="text/csv" name="userfile" id="csvfile" />
  26. </labeL>
  27. <p> </p>
  28. <p id="file-message"></p>
  29. <input type="submit" name="action" id="btnSend" value="Send" />
  30.  
  31. </form>
  32.  
  33. <form action="download.php" method="post" value="download">
  34. <p></p>
  35.  
  36. <input type="submit" name="btnDownload" id="btnDownload" value="Download PDF File" <?php if ((strcmp($message,$userMessage)!=0)|| (empty($userFile))){ ?> disabled <?php } ?> />
  37.  
  38. </form>
  39.  
  40.  
  41.  
  42.  
  43.  
  44. <?php
  45.  
  46. $upload_time = time() - $_SERVER['REQUEST_TIME'];//not used. gets time from the server
  47.  
  48. if(isset($_POST['action']) && $_POST['action']=='Send'):
  49. $userFile=$_FILES['userfile'];
  50. $name = $userFile['name'];
  51. $tmp = $userFile ['tmp_name'];
  52.  
  53.  
  54. $extension=explode('.', $name);
  55. $ext=end($extension);
  56.  
  57. $newName =md5($name).'.'.$ext;
  58.  
  59. $newName= str_replace(' $name ','$name','uploadFile').'.'.$ext;
  60. $message="";
  61. $userMessage="File uploaded successfuly ";
  62. $userMessageError= "Select a CSV file ";
  63. $userMessageFileError= "Invalid CSV file. Select a new file.";
  64. $userTimeMessage= "SESSION EXPIRED. Please upload a file again.";
  65. global $time_out;
  66.  
  67.  
  68.  
  69.  
  70. function validateFile($fileName)
  71. {
  72. $allowedCol=10;
  73. $isValid=false;
  74. if(!file_exists($fileName) || !is_readable($fileName)) return false;
  75.  
  76. $header = null;
  77. $data = array();
  78. $lines = file($fileName);
  79.  
  80. foreach($lines as $line) {
  81. $values = str_getcsv($line,',','"', '\');
  82. if(!$header) $header = $values;
  83. else $data[] = array_combine($header, $values);
  84. }
  85. if(count($data[0])==$allowedCol){
  86. $isValid= true;
  87. }else{
  88. $isValid= false;
  89. }
  90. return $isValid;
  91. }
  92.  
  93.  
  94.  
  95. if(empty($userFile)):
  96.  
  97. echo 'Select an file to upload';
  98. else:
  99. if((move_uploaded_file($tmp, 'uploads/'.$newName)) AND ($ext=='csv') AND (validateFile('uploads/'.$newName))):
  100.  
  101. ?>
  102.  
  103. <p id="info-message"> <?php echo $message=$userMessage; ?> </p>
  104. <p id="sec"> </p>
  105.  
  106. <?php
  107.  
  108.  
  109. else:
  110. if(($ext=='csv' && $ext!=null && !(validateFile('uploads/'.$newName))) OR ($ext!='csv' AND $ext!=null)): //if not a csv file, remove file from the folder
  111. ?>
  112. <p id="info-message"> <?php echo $message=$userMessageFileError; unlink('uploads/'.$newName)?> </p>
  113. <?php
  114. else:
  115. ?>
  116. <p id="info-message"> <?php echo $message=$userMessageError;?> </p>
  117. <?php
  118. endif;
  119. endif;
  120. endif;
  121.  
  122. endif;
  123.  
  124. ?>
  125.  
  126.  
  127.  
  128.  
  129.  
  130. <!-- shows the error and upload message for 30 secs. -->
  131. <script>
  132. setTimeout(function(){
  133. document.getElementById('info-message').style.display = 'none';
  134. }, 3000);
  135.  
  136. setTimeout(function(){
  137. document.getElementById('info-message-time').style.display = 'none';
  138. }, 3000);
  139.  
  140. <!-- wait 120 secs to enable download button. -->
  141. <?php
  142. if(($ext=='csv') AND (validateFile('uploads/'.$newName))){
  143. ?>
  144. document.getElementById('btnDownload').disabled = true;
  145. setTimeout(function(){
  146. document.getElementById('btnDownload').disabled = false;
  147. }, 3000);
  148.  
  149. <?php } ?>
  150.  
  151. <!-- desactivates download button after 15 sec after the file download. -->
  152. <?php
  153.  
  154. if(strcmp($message,$userMessage)==0){
  155. ?>
  156. setTimeout(function(){
  157. document.getElementById('btnDownload').disabled = true;
  158. }, 15000);
  159.  
  160. <?php } ?>
  161.  
  162. </script>
  163.  
  164. <!-- Find the distance between now and the count down date-->
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. <!-- allows only csv files. -->
  172. <script>
  173. var _validFileExtensions = [".csv"];
  174. function validate(oForm) {
  175. var arrInputs = oForm.getElementsByTagName("input");
  176. for (var i = 0; i < arrInputs.length; i++) {
  177. var oInput = arrInputs[i];
  178. if (oInput.type == "file") {
  179. var sFileName = oInput.value;
  180. if (sFileName.length > 0) {
  181. var blnValid = false;
  182. for (var j = 0; j < _validFileExtensions.length; j++) {
  183. var sCurExtension = _validFileExtensions[j];
  184. if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
  185. blnValid = true;
  186. break;
  187. }
  188. }
  189.  
  190. if (!blnValid) {
  191. alert("Invalid file type.");
  192. $('input[type=submit]').prop('disabled', true);
  193. return false;
  194. }
  195.  
  196. }
  197. }
  198. }
  199. return true;
  200. }
  201. }
  202.  
  203. </script>
  204.  
  205.  
  206.  
  207.  
  208.  
  209. <script>
  210.  
  211. var countDownDate=new Date();
  212. var timeTotal=15;
  213.  
  214. var x = setInterval(function() {
  215. var now = new Date().getTime();
  216.  
  217. // Find the distance between now and the count down date
  218. var distance =now-countDownDate;
  219. var seconds = Math.floor((distance % (1000 * 60)) / 1000);
  220. var downSeconds=(seconds-timeTotal)*-1;
  221.  
  222.  
  223. document.getElementById("sec").innerHTML = "Your session will expire in " + downSeconds + " seconds. ";
  224.  
  225. // If the count down is finished, write expired
  226. if (seconds> timeTotal) {
  227. clearInterval(x);
  228. document.getElementById("sec").innerHTML = "SESSION EXPIRED. Please upload a file again. ";
  229. }
  230.  
  231. }, 1000); //delay time to show message
  232.  
  233. </script>
  234.  
  235. <?php deleteFile();?>
  236.  
  237. <!--wait for 30 secs to delete the file if user does not click download -->
  238.  
  239. <?php
  240.  
  241. function deleteFile(){
  242. $time_out=false;
  243. $time_limit = 15000; //15 seconds
  244. clearstatcache();
  245. $current_time = time();
  246. $file_creation_time = filectime('uploads/'.$newName);
  247. clearstatcache();
  248.  
  249. echo $current_time;
  250. echo " ";
  251. echo $file_creation_time;
  252.  
  253.  
  254. $difference = $current_time - $file_creation_time;
  255.  
  256. $seconds = floor(($difference % (1000 * 60)) / 1000);
  257. //echo $difference;
  258.  
  259.  
  260. //difference
  261. if ((!empty($userFile))&& ($difference >= $time_limit)) {
  262.  
  263. unlink('uploads/'.$newName);
  264. }
  265. }
  266. ?>
  267.  
  268.  
  269. </body>
  270. </html>
Add Comment
Please, Sign In to add comment