You can create a file uploader that stores messages and files and sends you an email with the following steps:
-
Create an HTML form with input types for file and text message.
-
Use JavaScript to validate the form. You can make sure that all the required fields are filled in and that the file type is allowed.
-
Use PHP to handle the form submission. When the user submits the form, the PHP script should save the uploaded file and message in a database or a folder on the server.
-
Once the file and message are saved, use PHP's mail function to send an email to your email address with the details of the submitted file and message.
-
You can also add a message to the page to let the user know that their file and message have been uploaded and submitted successfully.
Here is a simple example of how to create a file uploader with the above steps:
HTML:
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Upload file:</label>
<input type="file" name="file" id="file"><br>
<label for="message">Message:</label>
<textarea name="message" id="message"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
JavaScript:
function validateForm() {
var file = document.getElementById("file");
var message = document.getElementById("message");
if (file.value == "") {
alert("Please select a file to upload.");
return false;
}
if (message.value == "") {
alert("Please enter a message.");
return false;
}
var allowedExtensions = /(\.pdf|\.doc|\.docx|\.txt|\.png|\.jpg|\.jpeg)$/i;
if (!allowedExtensions.exec(file.value)) {
alert("Please upload a file with a valid extension.");
file.value = "";
return false;
}
}
PHP:
<?php
if (isset($_POST["submit"])) {
$file = $_FILES["file"];
$message = $_POST["message"];
$fileName = $file["name"];
$fileTempName = $file["tmp_name"];
$fileSize = $file["size"];
$fileError = $file["error"];
$fileType = $file["type"];
$fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
if ($fileError === 0) {
$allowedExtensions = array("pdf", "doc", "docx", "txt", "png", "jpg", "jpeg");
if (in_array($fileExt, $allowedExtensions)) {
$fileNewName = uniqid("upload_", true) . "." . $fileExt;
$fileDestination = "uploads/" . $fileNewName;
move_uploaded_file($fileTempName, $fileDestination);
$to = "youremail@example.com";
$subject = "New file upload";
$body = "A new file has been uploaded with the following message:\n\n" . $message;
$headers = "From: uploader@example.com";
mail($to, $subject, $body, $headers);
echo "<p>Your file and message have been uploaded and submitted successfully.</p>";
} else {
echo "<p>Please upload a file with a valid extension.</p>";
}
} else {
echo "<p>There was an error uploading your file. Please try again.</p>";
}
}
?>
Note: In this example, the uploaded files are stored in a folder called "uploads". You will need to create this folder on your server and make sure it is writable. Also, note that this is a simple example and not necessarily secure. Make sure to add additional security measures according to your specific needs.