PHP上传图片未定义的索引问题

时间:2012-03-20 19:35:12

标签: php file upload

我有一个简单的HTML上传表单来上传图片和PHP脚本来上传它,但我总是得到这个错误,我看不出代码有什么问题。

注意:未定义的索引:第52行的public_html / lb / edit_profile.php中的fileField

HTML表单:

<fieldset class="step">
                        <form action="edit_profile.php" method="post" enctype="multipart/form-data">
                            <legend>Profile Picture</legend>
                            <p>
                                <label for="fileField">Upload (2MB Max.)</label>
                                <input name="fileField" type="file" id="fileField" />
                            </p>
                            <p>
                                <label for="updateBtn1">Please be patient...</label>
                               <input name="updateBtn1" type="submit" id="updateBtn1" value="Upload" />
                               <input name="parse_var" type="hidden" value="pic" />
                <input name="thisWipit" type="hidden" value="<?php echo $thisRandNum; ?>" />
                            </p></form>

PHP解析代码:

 <?php
include_once("scripts/checkuserlog.php");
?>
<?php
if (!isset($_SESSION['id']) || !isset($_SESSION['username'])){ 
   echo 'Please <a href="login.php">sign in</a> to edit your profile, settings and information.';
   exit(); 
}
$id = $logOptions_id; // Set the profile owner ID
$error_msg = ""; 
$errorMsg = "";
$success_msg = "";
$firstname = "";
$middlename = "";
$lastname = "";
$country = "";  
$state = "";
$city = "";
$zip = "";
$bio_body = "";
$bio_body = "";
$website = "";
$youtube = "";
$twitter = "";
$user_pic = "";
$cacheBuster = rand(9999999,99999999999); 
// ------- IF WE ARE PARSING ANY DATA ------------------------------------------------------------------------------------------------------------
if (isset($_POST['parse_var'])){

//// W.I.P.I.T ///
$thisWipit = $_POST['thisWipit'];
$sessWipit = base64_decode($_SESSION['wipit']);
//
if (!isset($_SESSION['wipit']) || !isset($_SESSION['idx'])) {
        echo  'Error: Your session expired from inactivity. Please <a href="index.php">click here to refresh it.</a>.';
        exit();
}
//
if ($sessWipit != $thisWipit) {
        echo  'Your session expired from inactivity. Please <a href="index.php">click here to refresh it.</a>.';
        exit();
}
//
if ($thisWipit == "") {
        echo  'Error: Missing Data... click back in your browser please.';
        exit();
}
//------------------------------------------------------------------------------------------------------------------------
// ------- PARSING PICTURE UPLOAD ---------
if (!isset($_POST['parse_var']))

if ($_POST['parse_var'] == "pic"){
    // If a file is posted with the form
    if ( isset($_FILES['fileField']) && ! empty($_FILES['fileField']['tmp_name']) ) {
            $maxfilesize = 2097152; // equal to 2 mb
            if($_FILES['fileField']['size'] > $maxfilesize ) { 

                        $error_msg = '<font color="#FF0000">ERROR: Your image was too large, please try again.</font>';
                        unlink($_FILES['fileField']['tmp_name']); 

            } else if (!preg_match("/\.(gif|jpg|png)$/i", $_FILES['fileField']['name'] ) ) {

                        $error_msg = '<font color="#FF0000">ERROR: Your image was not one of the accepted formats, please try again.</font>';
                        unlink($_FILES['fileField']['tmp_name']); 

            } else { 
                        $newname = "image01.jpg";
                        $place_file = move_uploaded_file( $_FILES['fileField']['tmp_name'], "members/$id/".$newname);
            }
    } 
}
// ------- END PARSING PICTURE UPLOAD ---------
//------------------------------------------------------------------------------------------------------------------------
// ------- PARSING PERSONAL INFO ---------
if ($_POST['parse_var'] == "location"){

    $firstname = preg_replace('#[^A-Za-z]#i', '', $_POST['firstname']); // filter everything but desired characters
    $lastname = preg_replace('#[^A-Za-z]#i', '', $_POST['lastname']); // filter everything but desired characters
    $country = strip_tags($_POST['country']);
    $country = str_replace("'", "&#39;", $country);
    $country = str_replace("`", "&#39;", $country);
    $country = mysql_real_escape_string($country);
    $state = preg_replace('#[^A-Z a-z]#i', '', $_POST['state']); // filter everything but desired characters
    $city = preg_replace('#[^A-Z a-z]#i', '', $_POST['city']); // filter everything but desired characters

    $sqlUpdate = mysql_query("UPDATE myMembers SET firstname='$firstname', lastname='$lastname', country='$country', state='$state', city='$city' WHERE id='$id' LIMIT 1");
    if ($sqlUpdate){
        $success_msg = '<img src="images/round_success.png" width="20" height="20" alt="Success" />Location information has been updated successfully.';
    } else {
        $error_msg = '<img src="images/round_error.png" width="20" height="20" alt="Failure" /> ERROR: Problems arose during the information exchange, please try again later.</font>';
    }
}
// ------- END PARSING LOCATION INFO ---------
//------------------------------------------------------------------------------------------------------------------------
// ------- PARSING LINKS ---------
if ($_POST['parse_var'] == "links"){

    $website = $_POST['website'];
    $website = strip_tags($website);
    $website = str_replace("http://", "", $website);
    $website = str_replace("'", "&#39;", $website);
    $website = str_replace("`", "&#39;", $website);
    $website = mysql_real_escape_string($website);
    $youtube = preg_replace('#[^A-Za-z_0-9]#i', '', $_POST['youtube']); // filter everything but desired characters
    $twitter = preg_replace('#[^A-Za-z_0-9]#i', '', $_POST['twitter']); // filter everything but desired characters

    $sqlUpdate = mysql_query("UPDATE myMembers SET website='$website', youtube='$youtube', twitter='$twitter' WHERE id='$id' LIMIT 1");
    if ($sqlUpdate){
            $success_msg = '<img src="images/round_success.png" width="20" height="20" alt="Success" />Your links and API connections have been updated successfully.';
    } else {
            $error_msg = '<img src="images/round_error.png" width="20" height="20" alt="Failure" /> ERROR: Problems arose during the information exchange, please try again later.</font>';
    }
}
// ------- END PARSING LINKS ---------
//------------------------------------------------------------------------------------------------------------------------
// ------- PARSING BIO ---------
if ($_POST['parse_var'] == "bio"){

    $bio_body = $_POST['bio_body'];
    $bio_body = str_replace("'", "&#39;", $bio_body);
    $bio_body = str_replace("`", "&#39;", $bio_body);
    $bio_body = mysql_real_escape_string($bio_body);
    $bio_body = nl2br(htmlspecialchars($bio_body));
     // Update the database data now here for all fields posted in the form
     $sqlUpdate = mysql_query("UPDATE myMembers SET bio_body='$bio_body' WHERE id='$id' LIMIT 1");
     if ($sqlUpdate){
            $success_msg = '<img src="images/round_success.png" width="20" height="20" alt="Success" />Your description information has been updated successfully.';
     } else {
            $error_msg = '<img src="images/round_error.png" width="20" height="20" alt="Failure" /> ERROR: Problems arose during the information exchange, please try again later.</font>';
     }
}
// ------- END PARSING BIO ---------
//------------------------------------------------------------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////////////
} // <<--- This closes "if ($_POST['parse_var']){"
// ------- END IF WE ARE PARSING ANY DATA ------------------------------------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// ------- ESTABLISH THE PROFILE INTERACTION TOKEN ---------
if (!isset($_SESSION['wipit'])) { // Check to see if session wipit is set yet
    session_register('wipit'); // Be sure to register the session if it is not set yet
}
$thisRandNum = rand(9999999999999,999999999999999999);
$_SESSION['wipit'] = base64_encode($thisRandNum);
// ------- END ESTABLISH THE PROFILE INTERACTION TOKEN ---------
$sql_default = mysql_query("SELECT * FROM myMembers WHERE id='$id'");

while($row = mysql_fetch_array($sql_default)){ 

    $firstname = $row["firstname"];
    $lastname = $row["lastname"];
    $country = $row["country"]; 
    $state = $row["state"];
    $city = $row["city"];
    $bio_body = $row["bio_body"];
    $bio_body = str_replace("<br />", "", $bio_body);
    $bio_body = stripslashes($bio_body);
    $website = $row["website"];
    $youtube = $row["youtube"];
    $twitter = $row["twitter"];
    ///////  Mechanism to Display Pic. See if they have uploaded a pic or not  //////////////////////////
    $check_pic = "members/$id/image01.jpg";
    $default_pic = "members/0/image01.jpg";
    if (file_exists($check_pic)) {
    $user_pic = "<img src=\"$check_pic?$cacheBuster\" width=\"190px\" />"; 
    } else {
    $user_pic = "<img src=\"$default_pic\" width=\"190px\" />"; 
    }


} 
?>

2 个答案:

答案 0 :(得分:0)

尝试关闭错误报告并查看其是否有效。如果在关闭错误报告后它确实有效,那么您的某个变量未正确设置。我会这样做:

       if (!isset($_POST['parse_var']))
       {
       $_POST['parse_var'] = "undefined";
       }    
       if($_POST['parse_var'] == "undefined")
       {
       //Code to handle undefined  exception
       }

if ($_POST['parse_var'] == "pic"){
    // If a file is posted with the form
    if ($_FILES['fileField']['tmp_name'] != "") { 
            $maxfilesize = 2097152; // equal to 2 mb
            if($_FILES['fileField']['size'] > $maxfilesize ) { 

                        $error_msg = '<font color="#FF0000">ERROR: Your image was too large, please try again.</font>';
                        unlink($_FILES['fileField']['tmp_name']); 

            } else if (!preg_match("/\.(gif|jpg|png)$/i", $_FILES['fileField']['name'] ) ) {

                        $error_msg = '<font color="#FF0000">ERROR: Your image was not one of the accepted formats, please try again.</font>';
                        unlink($_FILES['fileField']['tmp_name']); 

            } else { 
                        $newname = "image01.jpg";
                        $place_file = move_uploaded_file( $_FILES['fileField']['tmp_name'], "members/$id/".$newname);
            }
    } 
}

答案 1 :(得分:0)

更改

if ($_FILES['fileField']['tmp_name'] != "") { 

if ( isset($_FILES['fileField']) && ! empty($_FILES['fileField']['tmp_name']) ) { 
相关问题