Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- </style>
- <meta charset="UTF-8">
- <title> Generator</title>
- </head>
- <
- body>
- </span></span>
- <form action="" method="post" enctype="multipart/form-data" onsubmit="return validate(this)">
- <input type="hidden" name="<?php echo ini_get('session.upload_progress.prefix').ini_get('session.upload_progress.name'); ?>" value="123" />
- <labeL>
- <span>Choose CSV file:</span>
- </span></span>
- <input type="file" accept="text/csv" name="userfile" id="csvfile" />
- </labeL>
- <p> </p>
- <p id="file-message"></p>
- <input type="submit" name="action" id="btnSend" value="Send" />
- </form>
- <form action="download.php" method="post" value="download">
- <p></p>
- <input type="submit" name="btnDownload" id="btnDownload" value="Download PDF File" <?php if ((strcmp($message,$userMessage)!=0)|| (empty($userFile))){ ?> disabled <?php } ?> />
- </form>
- <?php
- $upload_time = time() - $_SERVER['REQUEST_TIME'];//not used. gets time from the server
- if(isset($_POST['action']) && $_POST['action']=='Send'):
- $userFile=$_FILES['userfile'];
- $name = $userFile['name'];
- $tmp = $userFile ['tmp_name'];
- $extension=explode('.', $name);
- $ext=end($extension);
- $newName =md5($name).'.'.$ext;
- $newName= str_replace(' $name ','$name','uploadFile').'.'.$ext;
- $message="";
- $userMessage="File uploaded successfuly ";
- $userMessageError= "Select a CSV file ";
- $userMessageFileError= "Invalid CSV file. Select a new file.";
- $userTimeMessage= "SESSION EXPIRED. Please upload a file again.";
- global $time_out;
- function validateFile($fileName)
- {
- $allowedCol=10;
- $isValid=false;
- if(!file_exists($fileName) || !is_readable($fileName)) return false;
- $header = null;
- $data = array();
- $lines = file($fileName);
- foreach($lines as $line) {
- $values = str_getcsv($line,',','"', '\');
- if(!$header) $header = $values;
- else $data[] = array_combine($header, $values);
- }
- if(count($data[0])==$allowedCol){
- $isValid= true;
- }else{
- $isValid= false;
- }
- return $isValid;
- }
- if(empty($userFile)):
- echo 'Select an file to upload';
- else:
- if((move_uploaded_file($tmp, 'uploads/'.$newName)) AND ($ext=='csv') AND (validateFile('uploads/'.$newName))):
- ?>
- <p id="info-message"> <?php echo $message=$userMessage; ?> </p>
- <p id="sec"> </p>
- <?php
- else:
- if(($ext=='csv' && $ext!=null && !(validateFile('uploads/'.$newName))) OR ($ext!='csv' AND $ext!=null)): //if not a csv file, remove file from the folder
- ?>
- <p id="info-message"> <?php echo $message=$userMessageFileError; unlink('uploads/'.$newName)?> </p>
- <?php
- else:
- ?>
- <p id="info-message"> <?php echo $message=$userMessageError;?> </p>
- <?php
- endif;
- endif;
- endif;
- endif;
- ?>
- <!-- shows the error and upload message for 30 secs. -->
- <script>
- setTimeout(function(){
- document.getElementById('info-message').style.display = 'none';
- }, 3000);
- setTimeout(function(){
- document.getElementById('info-message-time').style.display = 'none';
- }, 3000);
- <!-- wait 120 secs to enable download button. -->
- <?php
- if(($ext=='csv') AND (validateFile('uploads/'.$newName))){
- ?>
- document.getElementById('btnDownload').disabled = true;
- setTimeout(function(){
- document.getElementById('btnDownload').disabled = false;
- }, 3000);
- <?php } ?>
- <!-- desactivates download button after 15 sec after the file download. -->
- <?php
- if(strcmp($message,$userMessage)==0){
- ?>
- setTimeout(function(){
- document.getElementById('btnDownload').disabled = true;
- }, 15000);
- <?php } ?>
- </script>
- <!-- Find the distance between now and the count down date-->
- <!-- allows only csv files. -->
- <script>
- var _validFileExtensions = [".csv"];
- function validate(oForm) {
- var arrInputs = oForm.getElementsByTagName("input");
- for (var i = 0; i < arrInputs.length; i++) {
- var oInput = arrInputs[i];
- if (oInput.type == "file") {
- var sFileName = oInput.value;
- if (sFileName.length > 0) {
- var blnValid = false;
- for (var j = 0; j < _validFileExtensions.length; j++) {
- var sCurExtension = _validFileExtensions[j];
- if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
- blnValid = true;
- break;
- }
- }
- if (!blnValid) {
- alert("Invalid file type.");
- $('input[type=submit]').prop('disabled', true);
- return false;
- }
- }
- }
- }
- return true;
- }
- }
- </script>
- <script>
- var countDownDate=new Date();
- var timeTotal=15;
- var x = setInterval(function() {
- var now = new Date().getTime();
- // Find the distance between now and the count down date
- var distance =now-countDownDate;
- var seconds = Math.floor((distance % (1000 * 60)) / 1000);
- var downSeconds=(seconds-timeTotal)*-1;
- document.getElementById("sec").innerHTML = "Your session will expire in " + downSeconds + " seconds. ";
- // If the count down is finished, write expired
- if (seconds> timeTotal) {
- clearInterval(x);
- document.getElementById("sec").innerHTML = "SESSION EXPIRED. Please upload a file again. ";
- }
- }, 1000); //delay time to show message
- </script>
- <?php deleteFile();?>
- <!--wait for 30 secs to delete the file if user does not click download -->
- <?php
- function deleteFile(){
- $time_out=false;
- $time_limit = 15000; //15 seconds
- clearstatcache();
- $current_time = time();
- $file_creation_time = filectime('uploads/'.$newName);
- clearstatcache();
- echo $current_time;
- echo " ";
- echo $file_creation_time;
- $difference = $current_time - $file_creation_time;
- $seconds = floor(($difference % (1000 * 60)) / 1000);
- //echo $difference;
- //difference
- if ((!empty($userFile))&& ($difference >= $time_limit)) {
- unlink('uploads/'.$newName);
- }
- }
- ?>
- </body>
- </html>
Add Comment
Please, Sign In to add comment