How can PHP be used to display error messages and prompts for additional information when submitting a form with file uploads?
When submitting a form with file uploads in PHP, it is important to handle errors and prompt users for additional information if needed. This can be achieved by checking for errors in the file upload process, displaying appropriate error messages, and requesting additional information from the user if necessary.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"];
} else {
// Process file upload
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]);
echo "File uploaded successfully!";
}
}
?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload File">
</form>