Uploading a Photo
Please enter your name and choose a photo to upload, then click Send Photo.
Please enter your name and choose a photo to upload, then click Send Photo.
<?php
// Include the header file (assumed to contain common HTML and styles)
include('includes/header.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Uploading a Photo</title>
<link rel="stylesheet" type="text/css" href="css/common.css" />
</head>
<body>
<?php
// Check if the form has been submitted by looking for the "sendPhoto" POST field
if ( isset( $_POST["sendPhoto"] ) ) {
processForm();
} else {
displayForm();
}
// Function to handle the form submission and file upload
function processForm() {
// Check if a file was uploaded and if there were no errors
if ( isset( $_FILES["photo"] ) and $_FILES["photo"]["error"] == UPLOAD_ERR_OK ) {
// Check if the uploaded file is a JPEG image
if ( $_FILES["photo"]["type"] != "image/jpeg" ) {
echo "<p>JPEG photos only, thanks!</p>";
// Attempt to move the uploaded file to the "photos" directory
} elseif ( !move_uploaded_file( $_FILES["photo"]["tmp_name"], "photos/" . basename( $_FILES["photo"]["name"] ) ) ) {
// Display an error if file move fails, output the specific error code
echo "<p>Sorry, there was a problem uploading that photo.</p>" . $_FILES["photo"]["error"];
} else {
// Display a thank-you message if file upload is successful
displayThanks();
}
} else {
// Handle specific file upload errors
switch( $_FILES["photo"]["error"] ) {
case UPLOAD_ERR_INI_SIZE:
$message = "The photo is larger than the server allows.";
break;
case UPLOAD_ERR_FORM_SIZE:
$message = "The photo is larger than the script allows.";
break;
case UPLOAD_ERR_NO_FILE:
$message = "No file was uploaded. Make sure you choose a file to upload.";
break;
default:
$message = "Please contact your server administrator for help.";
}
// Display the error message to the user
echo "<p>Sorry, there was a problem uploading that photo. $message</p>";
}
}
// Function to display the file upload form
function displayForm() {
?>
<h1>Uploading a Photo</h1>
<p>Please enter your name and choose a photo to upload, then click Send Photo.</p>
<!-- Form for uploading photo, posts to "photo_upload.php" -->
<form action="photo_upload.php" method="post" enctype="multipart/form-data">
<div style="width: 30em;">
<!-- Set maximum file size to 50000 bytes -->
<input type="hidden" name="MAX_FILE_SIZE" value="50000" />
<!-- Input field for user's name -->
<label for="visitorName">Your name</label>
<input type="text" name="visitorName" id="visitorName" value="" />
<!-- File input field for the photo -->
<label for="photo">Your photo</label>
<input type="file" name="photo" id="photo" value="" />
<!-- Submit button to send the form -->
<div style="clear: both;">
<input type="submit" name="sendPhoto" value="Send Photo" />
</div>
</div>
</form>
<?php
}
// Function to display a thank-you message after successful upload
function displayThanks() {
?>
<h1>Thank You</h1>
<p>Thanks for uploading your photo<?php if ( $_POST["visitorName"] ) echo ", " . $_POST["visitorName"] ?>!</p>
<p>Here's your photo:</p>
<p><img src="photos/<?php echo htmlspecialchars($_FILES["photo"]["name"]) ?>" alt="Photo" /></p>
<?php
}
?>
</body>
</html>
<?php
// Include the footer file (assumed to contain closing HTML and scripts)
include('includes/footer.php');
?>