文本文件上传Ajax PHP

时间:2015-12-10 00:16:11

标签: php jquery ajax

我想上传不同的文本文件,并根据文本文件来源的机器类型使用不同的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');
     }
  });
});
});

这是我在http://www.formget.com/ajax-image-upload-php/

中搜索的代码参考

任何形式的帮助将不胜感激,并提前感谢您。

这是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>

这是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";
             }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果您使用的是IE8,请注意上述方法使用的是html5的FileReader API,IE8不支持:

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