上传时$ _POST和$ _FILES为空(甚至1 KB)

时间:2015-03-28 08:53:15

标签: php

我有一个您上传文件的页面。当你继续时,它说$ _POST没有设置(手动检查)。代码:

HTML(myCabinet.php):

<?php

    include_once 'include/check.php';   

?>


<html>
<head>
<title>TNet - My Cabinet</title>
<script type="text/javascript" src="/js/jquery.js" charset="utf-8"></script>
<style>
#topHeaderText {

    font-family: Comic Sans MS;
    font-size: 36px;
    color: #FFFFFF;
    margin-left: 10px; 
    margin-bottom: 25px;

}

.topHeader {

    text-align: left;
    position: relative;
    top: 5px;
    width: 1900px;
    height: 48px;
    background-color: #FF9100;
    margin-left: -10px;
    margin-top: 3px;

}

#bottomHeaderText {

    font-family: Comic Sans MS;
    font-size: 24px;
    color: #FFFFFF;
    margin-bottom: 25px;

}

.bottomHeader {

    text-align: center;
    position: relative;
    top: 12px;
    width: 1900px;
    height: 36px;
    background-color: #FF9100;
    margin-left: -10px;
    margin-top: 3px;

}

#mainBlock {

    width: 1900px;
    height: 1080px;
    background-color: #FF9100;
    margin-left: -10px;
    position: relative;
    top: 24px;

}

#profileImage {

    margin-top: 48px;
    margin-left: 48px;
    border-style: solid;
    border-width: 7px;
    border-color: #FFFFFF;

}

#profileTextLogin {

    font-family: Comic Sans MS;
    font-size: 20px;
    color: #FFFFFF;
    margin-left: 48px;

}

.profileText {

    font-family: Comic Sans MS;
    font-size: 16px;
    color: #FFFFFF;
    margin-left: 48px;

}

#settings {

    float: right;
    border-style: solid;
    border-width: 3px;
    border-color: #FFFFFF;
    height: 1020px;
    width: 600px;
    margin-right: 40px;
    margin-top: 36px;

}

#settingsTitle {

    margin-left: 24px;
    font-family: Comic Sans MS;
    font-size: 36px;
    color: #FFFFFF;

}

#settingsSetup {

    margin-left: 24px;
    font-family: Comic Sans MS;
    font-size: 16px;
    color: #FFFFFF;

}

.file {

    margin-left: 24px;
    font-family: Comic Sans MS;
    color: #FFFFFF;

}

.textInput {

    border-style: solid;
    border-color: #0004FF;
    color: #FF9100;
    font-family: Comic Sans MS;
    width: 40%;
    margin-left: 24px;

}

.button {

    border-style: solid;
    border-color: #0004FF;
    border-width: 3px;
    background-color: #FFFFFF;
    color: #FF9100;
    font-family: Comic Sans MS;

}

</style>

</head>
<body>

<div class="topHeader" id="topHeader">
<font id="topHeaderText">TNet | <a id="topHeaderText" href="/" style="margin-left: 0px;">Go out</a></font>
</div>
<div class="bottomHeader" id="bottomHeader">
<font id="bottomHeaderText"><a href="home.php" id="bottomHeaderText">HOME</a> | <a href="#" id="bottomHeaderText">MY PROFILE</a> | <a href="t!nser/index.php" id="bottomHeaderText">T!NSER</a> | <a href="home.php" id="bottomHeaderText">OTHER PROJECTS</a></font>
</div>
<div id="mainBlock">
<div id="settings">
<font id="settingsTitle">Account Settings</font>
<hr color="#FFFFFF"/>
<font id="settingsSetup">Change your image (128 x 128, *.png) or/and password</font><br/>
<form enctype="multipart/form-data" action="applySettings.php" method="post">
<input type="file" class="file" name="pimg"><br/>
<input type="password" placeholder="Type your new password" name="password" class="textInput"><br/>
<input type="password" placeholder="Confirm your new password" name="cpassword" class="textInput"><br/>
<input type="submit" name="submit" class="button" style="margin-left: 24px;" value="Submit settings">
</form>
<font id="settingsSetup">Delete your account</font><br/>
<form method="post" action="/login_register/deleteAccount.php" enctype="multipart/form-data">
<input type="submit" value="Delete your account" class="button" style="margin-left: 24px;" name="submit"><br/>
</form>
</div>
<img src="include/getimage.php" width="128" height="128" id="profileImage"><br/>
<font id="profileTextLogin"><?php echo $_SESSION['login'] ?></font><br/>
<font class="profileText">Role: <?php echo $_SESSION['role'] ?></font>
</div>
</body>
</html>

PHP(applySettings.php):

<?php

session_start();

if (isset($_POST['submit'])) {

    if (isset($_POST['pimg'])) {

        include_once 'include/dbcon.php';
        $imageData = mysql_real_escape_string(file_get_contents($_FILES["pimg"]["tmp_name"]));
        $imageType = mysql_real_escape_string($_FILES["pimg"]["type"]);
        $imgInfo = getimagesize($_FILES["pimg"]["tmp_name"]);

        echo "Hello";

        /*
        if (substr($imageType, 0, 5) == "image" && $imgInfo[0] == 128 && $imgInfo[1] == 128) {

            $query = mysql_query("UPDATE users_id SET image = '".$imageData."' WHERE username = ".$_SESSION['login']."'");
            if (!$query) {

                header('Location: /error/error.php?mysql_query_failed');
                die();

            }

            $_SESSION['image'] = $imageData;

            header('Location: myCabinet.php');
            die();

        } else {

            header('Location: /error/error.php?incorrectFileFormat');
            die();

        }

        */

    } else {

        echo "Is not set!";

    }

} else {

    echo "Submit not set!";

}


?>

非常感谢帮助。 如您所见,我的enctype是 multipart / form-data 。 POST_MAX_SIZE = 32M UPLOAD_MAX_SIZE = 32M

1 个答案:

答案 0 :(得分:2)

<input type="file" class="file" name="pimg">使用$ _FILES变量提交,可以在 applySettings.php 文件中访问。所以,数据看起来像这样

print_r($ _ FILES)将打印:

Array
(
    [pimg] => Array
        (
            [name] => v2.jpg
            [type] => image/jpeg
            [tmp_name] => C:\bin\xampp\tmp\phpA63B.tmp
            [error] => 0
            [size] => 8447
        )

)

所以,你的代码会改变

<?php
session_start();
if (isset($_POST['submit'])) {
    if (isset($_POST['pimg'])) {

<?php
session_start();
if (isset($_POST['submit'])) {
    if (isset($_FILES['pimg'])) {

希望,它可以帮助你