2012-03-20 109 views
0

我有一個簡單的HTML上傳表單上傳圖片和PHP腳本來上傳它,但我總是得到這個錯誤,我看不出任何錯誤的代碼。PHP上傳圖片undefined索引問題

注意:未定義指數:在FileField字段的public_html /磅/ edit_profile.php上線52

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\" />"; 
    } 


} 
?> 
+4

什麼線是52? – 2012-03-20 19:39:07

+0

是的,這是第52行,你可以做print_r $ _FILES – 2012-03-20 19:47:07

+0

第53行是if($ _FILES ['fileField'] ['tmp_name']!=「」){ – James 2012-03-20 19:51:11

回答

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); 
      } 
    } 
} 
+0

仍然發生同樣的錯誤! – James 2012-03-20 19:48:43

+0

嘗試添加此行到您的PHP:'error_reporting(E_ALL^E_NOTICE);' – 2012-03-20 19:51:01

+0

我補充說,錯誤沒有顯示,但圖像沒有上傳 – James 2012-03-20 19:54:05

0

變化

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

if (isset($_FILES['fileField']) && ! empty($_FILES['fileField']['tmp_name'])) { 
+0

該行現在已被添加並且錯誤不顯示,但現在該文件不上載 – James 2012-03-20 20:00:27

+0

嘗試刪除如果(!isset($ _ POST ['parse_var']))或至少刪除! – 2012-03-20 20:14:51