PHP 未定義索引錯誤 $_FILES?
2023-09-21
php問題
html5模板網
PHP Undefined index error $_FILES?(PHP 未定義索引錯誤 $_FILES?)
本文介紹了PHP 未定義索引錯誤 $_FILES?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我是 PHP 新手,正在學習 YouTube 上的教程.除了文件上傳之外,我在這個文件中的所有內容都在工作,任何幫助將不勝感激.這是我得到的錯誤:
*注意:我已經找了很多次了,但找不到與 $_FILES 相關的未定義索引錯誤...
<塊引用>
注意:未定義索引:第 95 行/Applications/xxx 中的頭像
注意:未定義索引:第 96 行/Applications/xxx 中的頭像
注意:未定義索引:第 97 行/Applications/xxx 中的頭像
注意:未定義索引:第 98 行/Applications/xxx 中的頭像
對不起,如果這是一個簡單的修復...
<?php//de-bugging 在腳本按需要工作后刪除它>錯誤報告(E_ALL);ini_set(display_errors", 1);//結束調試//$form = "<form action='register.php' method='post'><table cellpacing='5px'><tr><td></td><td><font color='red'>*</font>是必填字段.</td></tr><tr><td>名字:</td><td><input type='text' name='firstname' class='textbox'><font color='red'>*</font></td></tr><tr><td>姓氏:</td><td><input type='text' name='lastname' class='textbox'><font color='red'>*</font></td></tr><tr><td>用戶名:</td><td><input type='text' name='username' class='textbox'><font color='red'>*</font></td></tr><tr><td>電子郵件:</td><td><input type='text' name='email' class='textbox'><font color='red'>*</font></td></tr><tr><td>密碼:</td><td><input type='password' name='password' class='textbox'><font color='red'>*</font></td></tr><tr><td>確認密碼:</td><td><input type='password' name='repassword' class='textbox'><font color='red'>*</font></td></tr><tr><td>個人資料圖片:</td><td><input name='avatar' type='file' ></td></tr><tr><td>個人資料消息:</td><td><textarea name='bio' cols='35' rows='5' class='textbox'></textarea></td></tr><tr><td></td><td><input type='submit' name='submitbtn' value='Submit' class='button'></td></tr></form>";如果 ($_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']);//頭像上傳$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) {要求(腳本/connect.php");$query = mysql_query("SELECT * FROM users WHERE username='$username'");$numrows = mysql_num_rows($query);如果($numrows == 0){$query = mysql_query("SELECT * FROM users WHERE email ='$email'");$numrows = mysql_num_rows($query);如果($numrows == 0){$pass = (md5(md5($password)));$date = date(F j, Y");如果($姓名){move_uploaded_file($tmpname, "avatars/$username.$ext");$avatar = "$username.$ext";}別的$avatar = "default_avatar.png";$code = substr(md5(rand(111111111111, 999999999999999999)), 2, 25);mysql_query("插入用戶值('', '$firstname', '$lastname', '$username', '$email', '$pass', '$avatar', '$bio', '','', '$code', '', '$date')");$webmaster = "xxxx";$subject = "xxxx";$headers = "From:xxx<$webmaster>";$message = "xxx";郵件($email,$subject,$message,$headers);echo "xxx";}別的echo "那封郵件已經被占用了.$形式";}別的echo "那個用戶名已經被占用了.$形式";}別的echo "您沒有輸入有效的電子郵件.$形式";}別的echo "您的密碼不匹配.$形式";}別的echo "您沒有填寫所有必填字段.$形式";}別的echo "$form";?>
<?php require("styles/bottom.php") ;?>
解決方案
第一:盡量嚴格編程
error_reporting(E_ALL | E_STRICT);
你還必須使用isset來檢查數組是否可用
if (isset($_POST['submitbtn']) &&isset($_FILES['avatar'])) {//...}
還要檢查php配置
file_uploads "1"upload_max_filesize "2M"post_max_size "8M"max_file_uploads 20
帖子最大大小必須大于上傳最大文件大小.
也正如人們所說的檢查表單enctype
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...
<?php $title = "Register";?>
<?php require ("styles/top.php") ; ?>
<?php //de-bugging remove this after script works as desired>
error_reporting(E_ALL);
ini_set("display_errors", 1);
//end de-bugging//
$form = "<form action='register.php' method='post'>
<table cellspacing='5px'>
<tr>
<td></td>
<td><font color='red'>*</font> are required fields.</td>
</tr>
<tr>
<td>First Name:</td>
<td><input type='text' name='firstname' class='textbox'><font color='red'> *</font></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type='text' name='lastname' class='textbox'><font color='red'> *</font></td>
</tr>
<tr>
<td>Username:</td>
<td><input type='text' name='username' class='textbox'><font color='red'> *</font></td>
</tr>
<tr>
<td>Email:</td>
<td><input type='text' name='email' class='textbox'><font color='red'> *</font></td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' name='password' class='textbox'><font color='red'> *</font></td>
</tr>
<tr>
<td>Confirm Password:</td>
<td><input type='password' name='repassword' class='textbox'><font color='red'> *</font></td>
</tr>
<tr>
<td>Profile Picture:</td>
<td><input name='avatar' type='file' ></td>
</tr>
<tr>
<td>Profile Message:</td>
<td><textarea name='bio' cols='35' rows='5' class='textbox'></textarea></td>
</tr>
<tr>
<td></td>
<td><input type='submit' name='submitbtn' value='Submit' class='button'></td>
</tr>
</table>
</form>";
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";
?>
</div>
<?php require ("styles/bottom.php") ; ?>
解決方案
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
這篇關于PHP 未定義索引錯誤 $_FILES?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!