$ _FILES [" file"] [" name"]返回空值

时间:2014-07-12 14:31:31

标签: php html file upload

好吧,我正在创建这个网站,允许用户直接将文件上传到服务器而无需登录。除了一个小问题外,一切都很好(到目前为止)。当我记录IP时,我希望它打印用户IP和他们上传的文件名。除文件名外,一切都有效。我检查了每个可能的问题并搜索了几个小时。它本可以做的一切都没有成功。请查看我的内容,如果您发现问题,请告诉我。谢谢!

如果您有点好奇,网站为http://box.endurehosting.com/

<html>

<head>

<title>EndureBox | Upload</title>

<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" />

 <style type="text/css">

@import url("webfonts/Muli/fontstylesheet.css");

/*<![CDATA[*/

        .TitleText 
            {
                font-family: Muli;
                font-size: 58px;
                font-style: normal;
                line-height: normal;
                font-weight: normal;
                font-variant: normal;
                color: #FFF;
                text-shadow: 0px 0px 10px #707070;
            }

        .NormalText 
            {
                font-family: Muli;
                font-size: 18px;
                font-style: normal;
                line-height: normal;
                font-weight: normal;
                font-variant: normal;
                color: #FFF;
                text-shadow: 0px 0px 6px #999;
            }

        .SuccessText 
            {
                font-family: Muli;
                font-size: 30px;
                font-style: normal;
                line-height: normal;
                font-weight: normal;
                font-variant: normal;
                color: #83F52C;
                text-shadow: 0px 0px 6px #83F52C;
            }

        .FailedText     
            {
                font-family: Muli;
                font-size: 30px;
                font-style: normal;
                line-height: normal;
                font-weight: normal;
                font-variant: normal;
                color: #FF0000;
                text-shadow: 0px 0px 6px #FF0000;
            }

        .FileText 
            {
                font-family: Muli;
                font-size: 14px;
                font-style: normal;
                line-height: normal;
                font-weight: normal;
                font-variant: normal;
                color: #FFF;
                text-shadow: 0px 0px 6px #999;
            }

        .BlackText 
            {
                font-family: Muli;
                font-size: 19px;
                font-style: normal;
                line-height: normal;
                font-weight: normal;
                font-variant: normal;
                color: #000000;
                text-shadow: 0px 0px 3px #999999;
            }

    /*]]>*/

body {background-image:url("images/background.png");}

</style>

</head>

<body>

<div align="center">

</br><h1 class="TitleText">Endure Box</h1>

<?php 
if ($_POST)
    {
        $folder = "box/"; 
        $redirect = "index.php?complete";

        move_uploaded_file($_FILES["file"]["tmp_name"], "$folder" . $_FILES["file"]["name"]);

        header('Location: '.$redirect);
    }

if (isset($_GET['complete'])) 
    {
        if ($_FILES["file"]["error"] > 0)
            {
                echo "<span class='notice'><p class='SuccessText'>Upload Failed!</p></span>";
                echo "<span class='notice'><p class='NormalText'>" . $_FILES["file"]["error"] . "</p></span>";
            }
        else
            {
                echo "<span class='notice'><p class='SuccessText'>Upload Successful!</p></span>";

                $ipLog="log.txt";
                $friendly_name = $_FILES["file"]["name"];
                $register_globals = (bool) ini_get('register_gobals'); 

                if ($register_globals)
                {
                    $ip = getenv(REMOTE_ADDR); 
                }
                else 
                {
                    $ip = $_SERVER['REMOTE_ADDR'];
                }           

                $date=date ("l dS of F Y h:i:s A");
                $log=fopen("$ipLog", "a+"); 

                if (preg_match("/\bhtm\b/i", $ipLog) || preg_match("/\bhtml\b/i", $ipLog))  
                    { 
                        fputs($log, "File: $friendly_name | IP Address: $ip | Date Uploaded: $date <br>"); 
                    } 
                    else
                    {
                        fputs($log, "File: $friendly_name | IP Address: $ip | Date Uploaded: $date \n"); 
                    }

                fclose($log);
        }
    }
?>

<form action="index.php" method="post" enctype="multipart/form-data">

<p class="NormalText">Choose a file to upload</p></br>
<input type="file" name="file" id="file" class="FileText"/></br></br></br>
<input type="submit" name="submit" class="BlackText" value="Submit"/></br></br></br>

</form>

<a href="contents.php"> <img border="0" src="/images/box.png" alt="Open Box" width="200" height="200"></a></br></br></br>

<a href="contents.php" class="NormalText">Open Box</a>

</div>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

在您的情况下,问题出在以下一行:

header('Location: '.$redirect);

首次运行move_uploaded_file然后使用header函数进行重定向时,$_FILES数组变为空,因此在下一行简单中,您无法再检查$_FILES

此外,我没有看到任何重点使这个重定向。当您move_uploaded_file成功时,它会返回true,因此您可以更改以下代码:

<?php 
if ($_POST)
    {
        $folder = "box/"; 
        $redirect = "index.php?complete";

        move_uploaded_file($_FILES["file"]["tmp_name"], "$folder" . $_FILES["file"]["name"]);

        header('Location: '.$redirect);
    }

if (isset($_GET['complete'])) 

<?php 

$uploadStatus = false;
if ($_POST)
    {
        $folder = "box/"; 

       $uploadStatus = move_uploaded_file($_FILES["file"]["tmp_name"], "$folder" . $_FILES["file"]["name"]);

    }

if ($uploadStatus)