I'm trying to create an edit profile image where the user can edit stored profile, such as name, email etc, and also, update profile picture. However, whenever I try to upload a picture, while it exists in print_r I always get the "image empty" echo.
My code is this:
$fname = $lname = $email = $userPw = $title = $link = $description = $target_file = "";
$fnameError = $lnameError = $emailError = $pwError = $linkError = $descError = $uploadErr = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if(isset($_FILES["fileToUpload"]))
{
include ('../config/imageCheck.php');
echo 'image is not empty';
}
else
{
echo 'image empty';
}
//rest of the code checks and db query
imageCheck is:
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check == false)
{
echo $uploadErr = "File is not an image.";
}
if (file_exists($target_file))
{
echo $uploadErr = "File already exists.";
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" )
{
echo $uploadErr = "Only JPG, JPEG, PNG & GIF files are allowed.";
}
if ($_FILES["fileToUpload"]["size"] > 200000)
{
echo $uploadErr = "Your file is too large.";
}
and the form:
<form method="post" action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>>
<input type="text" class="form-control" name="fname" id="fname" value="<?php echo $fname;?>" onkeyup="validateLetter(this.id)" placeholder="Enter First Name" required autofocus><span class="error"><?php echo $fnameError;?></span>
<input type="text" class="form-control" name="lname" id="lname" value="<?php echo $lname;?>" onkeyup="validateLetter(this.id)" placeholder="Enter Last Name" required>
<input type="file" class="form-control" name="fileToUpload" id="fileToUpload">
<input type="submit" class="btn btn-primary" value="Sumbit">
</form>
If I change
if(isset($_FILES["fileToUpload"])
to
if(isset($_POST["fileToUpload"])
then I do get the "image is not empty echo" but imageCheck fails greatly, saying that
Notice: Undefined index: fileToUpload in C:\xampp\htdocs\ResearchWebsite\config\imageCheck.php on line 4
Notice: Undefined index: fileToUpload in C:\xampp\htdocs\ResearchWebsite\config\imageCheck.php on line 7
Warning: getimagesize(): Filename cannot be empty in C:\xampp\htdocs\ResearchWebsite\config\imageCheck.php on line 7 C:\xampp\htdocs\ResearchWebsite\config\imageCheck.php on line 27
Any help would be greatly appreciated! I'm at the brink of throwing my pc out of the window!