如何使用PHP读取/提取上传到HTML表单的文本文件的值到另一个文本文件中



>我有以下HTML,JavaScript和PHP代码来上传一个带有表格的文本文件并将其显示在同一页面中,

.HTML:

        <panel action="#" method="post"
  enctype="multipart/form-data">
  <label for="file">Filename:</label>
 <input type="file" name="file" id="file" onchange="return ajaxFileUpload(this);">

 </panel>
<iframe width="300px" height="300px" name="upload_iframe" id="upload_iframe" ></iframe>

JavaScript:

         function ajaxFileUpload(upload_field)
        {

        var filename = upload_field.value;

        upload_field.form.action = 'upload_file.php';
        upload_field.form.target = 'upload_iframe';
        upload_field.form.submit();     

        return true;
        }

PHP:upload_file.php

          <?php
       if ($_FILES["file"]["error"] > 0)
        {
         echo "Error: " . $_FILES["file"]["error"] . "<br>";
         } 
         else
           {
           $filepath = $_FILES["file"]["name"];
             if (file_exists($filepath)) {
          $file = fopen($filepath, 'r');
           echo '<table border=1>';
            while (!feof($file)) {
           $line = fgets($file);
        $first_char = $line[0];
        if ($first_char != '*' && $first_char != '^' && trim($line) != '') {
            if (strstr($line, '|')) {
                $split = explode('|', $line);
                echo '<tr>';
                foreach($split as $line) {
                    echo '<td>'.$line.'</td>';
                }
                echo '</tr>';
            } else {
                echo '<tr><td>'.$line.'</td></tr>';
             }
             }
      }
      echo '</table>';
         } else {
       echo 'the file does not exist';
      }
      }
       ?> 

现在,我有以下 php 代码来读取/提取文件内容并将其显示在另一个文本文件中,但我无法使用此代码获取输出。当我尝试从 html 页面中的输入中读取/提取值时,此代码工作正常,我的想法是否正确?我怎样才能做到这一点?

.PHP:

 file_put_contents($file, "n File Contents: ", FILE_APPEND | LOCK_EX);
//$ret = file_put_contents($file, $_POST['file'], FILE_APPEND | LOCK_EX);

ajaxFileUpload 无法发送 $_FILESvariable(仅适用于 exporer 或旧浏览器(

我建议你用正常的文件输入上传你的文件。您无需在发送表单后将上传的文件复制到目录中。

仅获取file_get_contents为 $_FILES["文件"]

["tmp_name"]的临时文件内容;

最新更新