0

所以我在我的网站上有一个文件上传功能,而不是重定向到 php 脚本页面,我想要一个 iframe 来显示脚本返回的消息。所以这是我的html:

<iframe class="iframe" name="my_iframe" src="upload_file.php" style="display:none;"></iframe>
<form id="uploadForm" action="upload_file.php" method="post" enctype="multipart/form-data" target="my_iframe">

所以基本上我想隐藏我的 iframe,直到上传过程完成并且脚本返回一条消息。这是我的 php 脚本:

<?php
    ini_set('display_errors', 'On');
    error_reporting(E_ALL | E_STRICT);
    session_start();
    $allowedExts = array("doc", "docx");
    $extension = pathinfo( $_FILES["upload"]["name"],PATHINFO_EXTENSION);
    $username = $_SESSION["username"];

    if (($_FILES["upload"]["size"] < 200000)
    && in_array($extension, $allowedExts)) {
        if ($_FILES["upload"]["error"] > 0)
        {
            echo "Return Code: " . $_FILES["upload"]["error"] . "<br />";
        }
        else
        {
            echo "Upload: " . $_FILES["upload"]["name"] . "<br />";
            echo "Type: " . $_FILES["upload"]["type"] . "<br />";
            echo "Size: " . ($_FILES["upload"]["size"] / 1024) . " Kb<br />";
            echo "Temp file: " . $_FILES["upload"]["tmp_name"] . "<br />";

            $dir_exists = is_dir("/disks/*/*/*/*/". $_SESSION["FirstName"] ."-".$_SESSION["username"]."/");
            $file_exists = file_exists("/disks/*/*/*/*/".$_SESSION["FirstName"] ."-".$_SESSION["username"]."/" . $_FILES["upload"]["name"]);
            $folderName=$_SESSION["FirstName"];
            $baseDir = "/disks/*/*/*/*/";
            // Create directory if it does not exist
            if (! $dir_exists) {
                if (is_writable($baseDir)) {
                    mkdir($baseDir . $_SESSION["FirstName"]."-".$_SESSION["username"]);
                } else {
                    trigger_error($baseDir.  " is not writeable");
                }
            }


            if ($file_exists) {
                echo $_FILES["upload"]["name"] . " already exists. ";
            } else {
                $link = new PDO('mysql:host=***;dbname=***;charset=UTF-8','***','***');
                $proptype = $_POST["prop_cat"];
                $stmt = $link->prepare("UPDATE Table SET `PType`=:proptype WHERE Username=:username");
                $stmt->bindParam(':username', $username);
                $stmt->bindParam(':proptype', $proptype);
                $stmt->execute();
                move_uploaded_file($_FILES["upload"]["tmp_name"],
                $baseDir. $_SESSION["FirstName"] ."-".$_SESSION["username"]."/". $_FILES["upload"]["name"]);
                echo "Stored in: " . $baseDir. $_SESSION["FirstName"] ."-".$_SESSION["username"]."/". $_FILES["upload"]["name"];
            }
        }
    } else {
        echo "Invalid file";
    }
?>

所以基本上总结了我的问题:如何在上传完成之前隐藏 iframe,然后显示用户可以像警报框一样关闭的消息?

4

1 回答 1

0

您可以在上传结束时回显 iframe,而不是尝试隐藏它然后在顶部显示它吗?

if ($file_exists) {
            echo $_FILES["upload"]["name"] . " already exists. ";
        } else {
            $link = new PDO('mysql:host=***;dbname=***;charset=UTF-8','***','***');
            $proptype = $_POST["prop_cat"];
            $stmt = $link->prepare("UPDATE Table SET `PType`=:proptype WHERE Username=:username");
            $stmt->bindParam(':username', $username);
            $stmt->bindParam(':proptype', $proptype);
            $stmt->execute();
            move_uploaded_file($_FILES["upload"]["tmp_name"],
            $baseDir. $_SESSION["FirstName"] ."-".$_SESSION["username"]."/". $_FILES["upload"]["name"]);
            echo "Stored in: " . $baseDir. $_SESSION["FirstName"] ."-".$_SESSION["username"]."/". $_FILES["upload"]["name"];

            /// load the iframe now ///////////////
            echo '<iframe class="iframe" name="my_iframe" src="upload_file.php"></iframe>';
            ///////////////////////////////////////
         }
于 2012-10-08T20:29:29.993 回答