dougong8012 2015-12-10 00:16
浏览 55

文本文件上传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.

$(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/

Any kind of help will be appreciated and thank you in advance.

Here is the html part:

<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:

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条回答 默认 最新

  • doukan5332 2015-12-10 00:48
    关注

    In case you are using IE8, please know that the above method uses FileReader API of html5 and is not supported in IE8:

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

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探