0

我有以下 html 和 php 代码来读取 html 页面中上传的文件并在另一个新页面中显示其内容,但我希望在同一个 html 页面中显示文件内容而不打开新选项卡并刷新,我该怎么做达到那个目的?

HTML:

<html>
    <body>

        <form action="upload_file.php" method="post"
              enctype="multipart/form-data">
            <label for="file">Filename:</label>
            <input type="file" name="file" id="file"><br>
            <input type="submit" name="submit" value="Submit">
        </form>
   </body>
</html>

PHP:上传文件.php

<?php
    if ($_FILES["file"]["error"] > 0)
    {
        echo "Error: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
?>
4

2 回答 2

0

您可以使用 php 检查是否单击了提交按钮,然后继续显示数据。

PHP:上传.php

<?php
    //checks if the submit button is submitted
    if(isset($_POST['submit']) && $_POST['submit'] == "Submit") {
        if ($_FILES["file"]["error"] > 0) {
            echo "Error: " . $_FILES["file"]["error"] . "<br>";
        }
        else {
            echo "Upload: " . $_FILES["file"]["name"] . "<br>";
            echo "Type: " . $_FILES["file"]["type"] . "<br>";
            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
            echo "Stored in: " . $_FILES["file"]["tmp_name"];
        }
    }
?>
<html>
    <body>
        <form action="upload.php" method="post" enctype="multipart/form-data">
            <label for="file">Filename:</label>
            <input type="file" name="file" id="file"><br>
            <input type="submit" name="submit" value="Submit">
            </form>
    </body>
</html>
于 2013-09-19T18:02:34.390 回答
0

您可以使用 iframe

html

<html>
    <body>
        <script>
            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;
            }
        </script>
        <iframe name="upload_iframe" id="upload_iframe" ></iframe>

        <form action="#" method="post" enctype="multipart/form-data">
            <label for="file">Filename:</label>

            <input type="file" name="file" id="file" onchange="return ajaxFileUpload(this);">
        </form>
    </body>
</html>

上传文件.php

 <?php
     if ($_FILES["file"]["error"] > 0)
     {
         echo "Error: " . $_FILES["file"]["error"] . "<br>";
     }
     else
     {
         echo "Upload: " . $_FILES["file"]["name"] . "<br>";
         echo "Type: " . $_FILES["file"]["type"] . "<br>";
         echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
         echo "Stored in: " . $_FILES["file"]["tmp_name"]. " <br>";
         $content = file_get_contents($_FILES["file"]["tmp_name"]);

         echo "Content: ".$content; 
     }
   ?>
于 2013-09-19T18:23:49.973 回答