简体   繁体   English

文本文件上传Ajax PHP

[英]Text File Upload Ajax PHP

I want to upload different text files and process it with different php script based on the type of the machine that the text file came from. 我想上传不同的文本文件,并根据文本文件来自的计算机的类型,使用不同的php脚本对其进行处理。

$(document).ready(function(e){
$('#txtUpload').on('submit',function(e){
  var mType = $('#cmbmachine option:seleted').val();
  var page;

   if(mType == 'machine1')
   {
     page = 'machine1.php';
   }
   if(mType == 'machine2')
   {
     alert('To be added');
   }
  e.preventDefault();
  $.ajax({
     url: page,
     dataType: 'text',
     type: 'post',
     data: new FormData(this),
     contentType: false,
     cache: false,
     processData: false,
     success: function(data){
        alert('ok');
     }
  });
});
});

This is my code reference that I searched in http://www.formget.com/ajax-image-upload-php/ 这是我在http://www.formget.com/ajax-image-upload-php/中搜索到的代码参考

Any kind of help will be appreciated and thank you in advance. 任何帮助将不胜感激,并在此先感谢您。

Here is the html part: 这是html部分:

<fieldset>
<legend><strong>Select type of machine model</strong></legend>
<select id="cmbmachine" name="machine" class="field">
<option value="machine1">Machine 1</option>
<option value="machine2">Machine 2</option>
</select>
</fieldset>
<fieldset>
    <legend><strong>Select a file to upload</strong></legend>
    <form id="txtUpload" method="post" enctype="multipart/form-data">
    <input type="file" name="files[]" size="40" multiple="multiple" />
     <br />
      <p></p>
       <input type="submit" value="Upload File" onclick="myFunction()">
       <br />
        <br />
    </form>

</fieldset>
<fieldset>
<legend><strong>Uploaded Files</strong></legend>
    <div id="uploaded"></div>
</fieldset>
<script type="text/javascript">
document.getElementById("cmbmachine").value="<?php echo $_GET['machine'];?>";
</script>

And here is the php part: 这是PHP部分:

include 'functions.php';

set_time_limit(0);
if(isset($_FILES['files']))
{
foreach($_FILES['files']['tmp_name'] as$key => $tmp_name)
    {
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size = $_FILES['files']['size'][$key];
        $file_tmp = $_FILES['files']['tmp_name'][$key];
        $file_type = $_FILES['files']['type'][$key];

        if($file_size > 10000000) //10mb
        {
            echo "<script>alert('File exceeds file size')</script>";
        }

        if($file_type == "text/plain")
        {
            $i = 0;
             $file = fopen($file_tmp,"r");

             while(($data = fgetcsv($file, 1000, "\t"))!=FALSE)
             {
                if($i > 0)
                {
                    $data[0] = "";
                    $data[1] = "";
                    $data[3] = "";
                    $data[4] = "";
                    $data[5] = "";

                    unset($data[0],$data[1],$data[3],$data[4],$data[5]);
                     $line[] = $data;
                }
                $i++;
             }
             fclose($file);
             $j = 0;
             foreach($line as $value)
             {
                $newline = explode(" ",$value[6]);
                 $date  = trim($newline[0]);
                  $time = trim($newline[2]);
                   $newtime = date("H:i",strtotime($time));

                 try{
                    $query = $con->prepare("INSERT IGNORE INTO temp_logs(EmpID, ValidDate, ValidTime)VALUES(:id,:ddate,:time)");
                     $query->bindParam(':id',$value[2]);
                      $query->bindParam(':ddate',$date);
                       $query->bindParam(':time',$time);
                        $query->execute();
                 }
                 catch(PDOException $e){
                    echo $e->getMessage();
                     exit;
                 }
                 $j++;
                 echo $j . " row(s) processed.";

                 echo str_repeat(' ',1024 * 64);

                 flush();

                 sleep(0);
             }
             if($query)
             {
                echo "Process completed";
             }
        }
    }
}

In case you are using IE8, please know that the above method uses FileReader API of html5 and is not supported in IE8: 如果您使用的是IE8,请注意上述方法使用html5的FileReader API,并且IE8不支持该方法:

http://caniuse.com/#feat=filereader http://caniuse.com/#feat=filereader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM