0

我有以下 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:上传文件.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);
4

1 回答 1

0

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

我建议您使用普通文件输入上传您的文件。发送表单后,您不需要将上传的文件复制到目录。

仅获取 $_FILES["file"]["tmp_name"]; 的 file_get_contents 的临时文件内容;

于 2013-09-25T21:23:42.157 回答