dsfs23434 2013-09-25 21:11
浏览 90

如何使用php读取/提取上传到html表单的文本文件的值到另一个文本文件?

i have the following HTML, JavaScript and PHP code to upload a text file which has a table and display it in the same page,

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';
      }
      }
       ?> 

Now, i have the following php code to read/extract the file contents and display it in another text file, but i cannot get the output with this code. This code works fine when i tried to read/extract the values form an input in html page, am i thinking in the right direction ? how can i achieve this?

PHP:

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

1条回答 默认 最新

  • douganmo1121 2013-09-25 21:23
    关注

    ajaxFileUpload cannot send a $_FILESvariable (only with exporer or old browsers)

    I sugest you to Upload your file with normal file input. You don't need copy the uploaded file to a directory after sent form.

    Only get the temporany file contents with file_get_contents of $_FILES["file"]["tmp_name"];

    评论

报告相同问题?

悬赏问题

  • ¥15 使用yolov5-7.0目标检测报错
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备