Why I got Undefined array key and Trying to access array offset on value of type null Error when using $_FILES in PHP

I do this in my cms (Content Management System) in register section. I want to add profile image and I do like w3schools tuts: https://www.w3schools.com/php/php_file_upload.asp. I do it ant=other file it can but when I run in my file I get an error: Warning: Undefined array key "profileimg_regis" in C:\xampp\htdocs\DeknoinarakDev\dashboard\run\code.php on line 99
Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\DeknoinarakDev\dashboard\run\code.php on line 99
Warning: Undefined array key "profileimg_regis" in C:\xampp\htdocs\DeknoinarakDev\dashboard\run\code.php on line 103
Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\DeknoinarakDev\dashboard\run\code.php on line 103
Fatal error: Uncaught ValueError: Path cannot be empty in C:\xampp\htdocs\DeknoinarakDev\dashboard\run\code.php:103 Stack trace: #0 C:\xampp\htdocs\DeknoinarakDev\dashboard\run\code.php(103): getimagesize('') #1 {main} thrown in C:\xampp\htdocs\DeknoinarakDev\dashboard\run\code.php on line 103
This is my PHP code:
// Check if image file is a actual image or fake image
if(isset($_POST["register_btn"])) {
$target_dir = "img/user/";
$target_file = $target_dir . basename($_FILES["profileimg_regis"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$check = getimagesize($_FILES["profileimg_regis"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
}
else {
echo "File is not an image.";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["profileimg_regis"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
echo "Sorry, only JPG, JPEG & PNG files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
}
else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
This is my HTML code:
<div class="modal-background" id="md-regisadmin">
<div class="modal-container">
<div class="modal">
<div class="modal-header">
<h2>Admin Account</h2>
<button onclick="closeModal(document.querySelectorAll('#md-regisadmin'))" type="button" id="dismiss-btn-modal">
<i class="fa fa-times" onclick="closeModal(document.querySelectorAll('#md-regisadmin'))" id="dismiss-btn-modal"></i>
</button>
</div>
<div class="md-content">
<div class="form-modal">
<div class="form-group md">
<form action="run/code.php" method="post">
<!--<label for="username_regis">Username:</label>
<input id="username_regis" type="text" name="username_regis" placeholder="Username" required/>
</div>
<div class="form-group md">
<label for="email_regis">E-mail:</label>
<input id="email_regis" type="email" name="email_regis" placeholder="Email" required/>
</div>
<div class="form-group md">
<label for="pwd_regis">Password:</label>
<input id="pwd_regis" type="password" name="pwd_regis" placeholder="Password" required/>
</div>
<div class="form-group md">
<label for="pwdr_regis">Repeat Password:</label>
<input id="pwdr_regis" type="password" name="pwdr_regis" placeholder="Repeat Password" required/>
</div>
<div class="form-group md">-->
<input id="profileimg_regis" name="profileimg_regis" type="file"/>
<!--</div>
<div class="form-group md">
<label for="utype_regis">Usertype:</label>
<select id="utype_regis" name="utype_regis"/>
<option value="noper">No Permission</option>
<option value="readonly">Reader</option>
<option value="mod">Moderator (Only Change Data In Website)</option>
<option value="admin">Administrator</option>
<option value="superadmin">Super Administrator</option>
<option value="godper">God Permission</option>
<option value="root">Root Administrator</option>
</select>
</div>
</div>
</div>-->
<div class="modal-footer">
<button type="submit" name="register_btn">
Add Admin Account
</button>
</form>
<button type="button" onclick="closeModal(document.querySelectorAll('#md-regisadmin'))" id="dismiss3-btn-modal">
<a onclick="closeModal(document.querySelectorAll('#md-regisadmin'))" id="dismiss2-btn-modal">Cancel</a>
</button>
</div>
</div>
</div>
</div>
Thanks if you can help. :)
Answer
Solution:
Now I find How to fix it.
I forgot<form>enctype="multipart/form-data"</form>
at the top of the form
Sorry. :)
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: npm err! code 1
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.