In what ways can the PHP script be modified to improve user experience and streamline the file upload process, including email notifications?
To improve user experience and streamline the file upload process, we can add email notifications to inform users about the status of their file uploads. This can be achieved by modifying the PHP script to send an email notification to the user once the file upload is successful. Additionally, we can enhance the user interface by providing feedback messages on the upload progress and completion.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_FILES["file"])) {
$targetDir = "uploads/";
$targetFile = $targetDir . basename($_FILES["file"]["name"]);
if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetFile)) {
// Send email notification to user
$to = "user@example.com";
$subject = "File Upload Notification";
$message = "Your file has been successfully uploaded.";
$headers = "From: webmaster@example.com";
mail($to, $subject, $message, $headers);
echo "File uploaded successfully. Check your email for confirmation.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>