I want to do an image file upload. I copied the code from W3S but get errors:
1- Notice: Undefined index: fileToUpload in C:\xampp\htdocs\Family Tree\FT_UploadFile.php on line 35
2- Notice: Undefined index: fileToUpload in C:\xampp\htdocs\Family Tree\FT_UploadFile.php on line 55
(Strangely, I don't get the same error on lines 40, 55, 70 and 71 even if there is reference to "fileToUpload")
3- Sorry, only JPG, JPEG, PNG & GIF files are allowed.Sorry, your file was not uploaded.
The file I'm trying to upload is definitely a "jpg" file as shown when I print the file name that was POSTed for uploading, i.e., C:\Users\Michael\Documents\PHOTOS\EASTER.jpg . Perhaps this is only due to the previous problem.
The code in question is:
$target_dir = "C:/xampp/htdocs/acconts/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); <---------- line 35
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); <---------- line 40
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 \"$target_file\" already exists.";
$uploadOk = 0;
}
// Check file size if ($_FILES["fileToUpload"]["size"] > 500000) { <---------- line 55
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "JPG" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "JPEG"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF 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)) { <---------- line 70 echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded."; <---------- line 71
} else {
echo "Sorry, there was an error uploading your file.";
}
}
Thank you.