What are some alternative methods for handling image attachments in PHP besides using a contact form?
When handling image attachments in PHP without using a contact form, one alternative method is to allow users to upload images directly to a server using a file upload form. This can be done by creating a simple HTML form with an input field of type "file" and handling the file upload in PHP using the $_FILES superglobal.
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>