Saturday, 27 January 2018

file - PHP Undefined index error $_FILES?



I am new to PHP and am following a tutorial on YouTube. I have everything working in this file, except for the file uploading, any help would be appreciated. Here is the error i am getting:



*NOTE: I have looked for this many times, but could not find undefined index error relevant to $_FILES...




Notice: Undefined index: avatar in /Applications/xxx on line 95



Notice: Undefined index: avatar in /Applications/xxx on line 96




Notice: Undefined index: avatar in /Applications/xxx on line 97



Notice: Undefined index: avatar in /Applications/xxx on line 98




Sorry for this if it is a simple fix...



            



error_reporting(E_ALL);
ini_set("display_errors", 1);
//end de-bugging//

$form = "















































* are required fields.
First Name: *
Last Name: *
Username: *
Email: *
Password: *
Confirm Password: *
Profile Picture:
Profile Message:

";



if ($_POST['submitbtn']) {

$firstname = strip_tags($_POST['firstname']);
$lastname = strip_tags($_POST['lastname']);
$username = strip_tags($_POST['username']);
$email = strip_tags($_POST['email']);
$class = ($_POST['class']);
$password = strip_tags($_POST['password']);

$repassword = strip_tags($_POST['repassword']);
$bio = strip_tags($_POST['bio']);
//AVATAR UPLOAD
$name = $_FILES['avatar'] ['name'];
$type = $_FILES['avatar'] ['type'];
$size = $_FILES['avatar'] ['size'];
$tmpname = $_FILES['avatar'] ['tmpname'];
$ext = substr($name, strrpos($name, '.'));




if ($firstname && $lastname && $username && $email && $password && $repassword) {
if ($password == $repassword) {
if (strstr($email, "@") && strstr($email, ".") && strlen($email) >= 6) {

require("scripts/connect.php");

$query = mysql_query("SELECT * FROM users WHERE username ='$username'");
$numrows = mysql_num_rows($query);
if ($numrows == 0) {


$query = mysql_query("SELECT * FROM users WHERE email ='$email'");
$numrows = mysql_num_rows($query);
if ($numrows == 0) {

$pass = (md5(md5($password)));
$date = date("F j, Y");

if($name){
move_uploaded_file($tmpname, "avatars/$username.$ext");

$avatar = "$username.$ext";
}
else
$avatar = "default_avatar.png";

$code = substr(md5(rand(111111111111, 99999999999999999)), 2, 25);


mysql_query("INSERT INTO users VALUES ('', '$firstname', '$lastname', '$username', '$email', '$pass', '$avatar', '$bio', '', '', '$code', '', '$date')");


$webmaster = "xxxx";
$subject = "xxxx";
$headers = "From:xxx<$webmaster>";
$message = "xxx";

mail($email, $subject, $message, $headers);

echo "xxx";

}

else
echo "That email is already taken. $form";
}
else
echo "That username is already taken. $form";

}
else
echo "You did not enter a valid email. $form";


}
else
echo "Your passwords did not match. $form";
}
else
echo "You did not fill in all of the required fields. $form";

}
else
echo "$form";


?>




Answer



first: try to strict programming



error_reporting(E_ALL | E_STRICT);



also you must use isset for check is index for array available or not



if (isset($_POST['submitbtn']) && isset($_FILES['avatar'])) {
// ...
}


also check php configuraion




file_uploads    "1"
upload_max_filesize "2M"
post_max_size "8M"
max_file_uploads 20


post max size must be larger than upload max file size.



also as guys said check form enctype



No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...