php - 显示ajax上传的txt文件内容

标签 php jquery html ajax file-upload

我已经能够使用 ajax 将 .txt 文件上传到我的网站,但我还想在我的网站上显示 .txt 文件的内容。我不知道该怎么做。谢谢!

这是我的html

<!DOCTYPE html>
<html>
    <head> 
            <script type="text/javascript" src="static/jquery-1.4.2.js"></script>
            <script type="text/javascript" src="static/jquery-ui-1.8.4.custom.min.js"></script>
            <script type='text/javascript' src='static/js_test.js'></script>
            <script type='text/javascript' src='static/jquery.form.js'></script> 

            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
            <script src="http://malsup.github.com/jquery.form.js"></script>
    </head>
    <body>    
            <form action="http://malsup.com/jquery/form/file-echo2.php" method="post" enctype="multipart/form-data">
                <input type="file" name="myfile"/>
                <input type="submit" value="Submit File"/>
            </form>



            <div class="progress">
                <div class="percent">0%</div >
            </div>

            <div id="status"></div>
    </body>
</html>

这是我的jquery

$(document).ready(function() {

(function() {

var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');

$('form').ajaxForm({
    beforeSend: function() {
        status.empty();
        var percentVal = '0%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    uploadProgress: function(event, position, total, percentComplete) {
        var percentVal = percentComplete + '%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    success: function() {
        var percentVal = '100%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
complete: function(xhr) {
    status.html(xhr.responseText);
}
}); 

})();       



});  

这是我的php

<?php
foreach($_FILES as $file) {
   $n = $file['name'];
    $s = $file['size'];
   if (is_array($n)) {
      $c = count($n);
      for ($i=0; $i < $c; $i++) {
         echo "<br>uploaded: " . $n[$i] . " (" . $s[$i] . " bytes)";
      }
   }
   else
      echo "<br>uploaded: $n ($s bytes)";
}
?>

最佳答案

您可以在 ajax 请求中将其发回:

在文件-echo2.php 中:

<?php

foreach($_FILES as $file) {
   $n = $file['name'];
   $s = $file['size'];

   if (is_array($n)) {
      $c = count($n);
      for ($i=0; $i < $c; $i++) {
         if (move_uploaded_file($n[$i], '/path/where/you/put/that/file.txt')) {
            echo "uploaded " . $n[$i] . " (" . $s[$i] . " bytes):\n";
            echo file_get_contents('/path/where/you/put/that/file.txt');
         }
      }
   }
   else {
      if (move_uploaded_file($n, '/path/where/you/put/that/file.txt')) {
            echo "uploaded " . $n . " (" . $s . " bytes):\n";
            echo file_get_contents('/path/where/you/put/that/file.txt');
      }
   }
}

关于php - 显示ajax上传的txt文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19386831/

相关文章:

php - 一个目录下有很多文件?

javascript - 添加监听器到div标签来观看innerHtml内容

html - Live Sass 编译器在最新的 VSCode 后导致扩展主机崩溃

javascript - 在填写联系表单后,如何让用户返回到他们原来的页面?

php - 如何在 Windows 上集成 PHP 和 R?

php - Cookie 在重定向时被删除

php - 根据特定 ID 按字母顺序选择用户

javascript - 我可以使用 javascript/jquery 在 iframe 中提交非本地表单吗?

jquery - 添加边框 div 将其他 div 推出盒子

javascript - 无法拖动工作