I am building a really simple edit profile using php.
I want the user to be able to fill their data and also upload an image.
but the image upload should not be compulsory. That is, the user can submit the form without uploading the image AND also the user can upload the image only without filling out data (Though, the php validation should also check for empty fields and errors).
This is my html
<form class="form" action="#" method="post" enctype="multipart/form-data"
id="registrationForm">
<div class="uploadpic">
<label class="btn btn-primary" for="my-file-selector">
<input id="my-file-selector" name="file" class="text-center center-block
file-upload" type="file" style="display:none"
onchange="$('#upload-file-info').html(this.files[0].name)">
Upload profile photo
</label>
<label for="email">
<h4>Email</h4>
</label>
<input type="text" class="form-control" name="email" id="email" value="<?php
echo $email ?>">
</div>
<div class="form-group">
<label for="number">
<h4>Phone number</h4>
</label>
<input type="number" class="form-control" name="number"
id="number" value="<?php echo $number ?>">
</div>
<input type="submit" class="btn btn-lg btn-success" name="submit"
value="Update">
This is my php
if (isset($_POST["submit"])) {
$fileExtensions = ['jpeg', 'jpg', 'png']; // Get all the file extensions
$fileName = $_FILES['file']['name'];
$fileSize = $_FILES['file']['size'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileType = $_FILES['file']['type'];
$fileExtension = strtolower(end(explode('.', $fileName)));
$uploadPath = $currentDir . $uploadDirectory . basename($fileName);
$target_file = $uploadDirectory . basename($_FILES["file"]["name"]);
number = clean($_POST["number"]);
$email = clean($_POST["email"]);
if (!in_array($fileExtension, $fileExtensions)) {
$errors[] = "This file extension is not allowed. Please upload a JPEG or PNG
file";
}
if ($fileSize > 2000000) {
$errors[] = "This file is more than 2MB. Sorry, it has to be less than or
equal to 2MB";
}
if (empty($email) || strlen($email) < 2) {
$errors[] = "Input your email address";
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "Invalid email address format";
}
$query = $pdo->prepare("SELECT email FROM users WHERE email = :email AND id
!= :sid");
$query->bindParam(':email', $email);
$query->bindParam(':sid', $userid);
$query->execute();
if ($query->rowCount() > 0) {
$errors[] = "Email address already registered";
}
if (empty($number) || strlen($number) < 10) {
$errors[] = "Write a valid phone number";
}
if (count($errors) == 0) {
$st = $pdo->prepare("UPDATE users SET email = :email, iname = :iname, ioname
= :ioname, number = :number WHERE id = :sid");
$st->bindParam(':email', $email);
$st->bindParam(':number', $number);
$st->bindParam(':iname', $target_file);
$st->bindParam(':ioname', $fileName);
$st->bindParam(':sid', $userid);
$st->execute();
move_uploaded_file($fileTmpName, $target_file);
}
else {
foreach($errors as $error) {
$display.= "* $error<br/>";
}
}
}
How