I checked many times and I think I did not miss anything but still the code does not work exactly, and when I click "sendPhoto" the browser gives me a message with that the URL are not found and I am sure I have the image in the root file.
this is the code if anybody have time to check it, thanks
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style rel="stylesheet" type="text/css" href="common.css"></style>
</head>
<body>
<?php
if ( isset($_POST["sendPhoto"]) ) {
processForm();
} else {
displayForm();
}
function processForm() {
if ( isset($_FILES["photo"]) and $_FILES["photo"]["error"]==UPLOAD_ERR_OK ) {
if ( $_FILES["photo"]["type"] != "image/jpeg" ) {
echo "<p>JPEG only allowed, thanks.</p>";
} elseif ( !move_uploaded_file( $_FILES["photo"]["tmp_name"], "photos/" . basename($_FILES["photo"]["name"]) ) ) {
echo "<p>Sorry there is something went wrong with uploading your photo !!!</p>" . $_FILES["photo"]["error"];
} else {
displayThanks();
}
} else {
switch ( $_FILES["photo"]["error"] ) {
case UPLOAD_ERR_INI_SIZE:
$message = "the photo is too large for the server !!!";
break;
case UPLOAD_ERR_FORM_SIZE:
$message = "the photo is too large for the script !!!";
break;
case UPLOAD_ERR_NO_FILE:
$message = "no file was upload. make sure you choose a file !!!";
break;
default:
$message = "Please contact your server help";
}
echo "<p> SORRY !!!, ther was a problem uploading your photo . $message</p>";
}
}
function displayForm() {
?>
<h1> Uploading a Photo </h1>
<p> Please enter your name and choose a photo to upload, then click send photo.</p>
<form action="photo-upload.php" method="post" enctype="multipart/form-data">
<div style="width:30em;">
<input type="hidden" name="MAX_FILE_SIZE" value="50000" />
<label for="visitorName">your name : </label>
<input type="text" name="visitorName" id="visitorName" value="" />
<label for="photo">your photo : </label>
<input type="file" name="photo" id="photo" value="" />
<div style="clear:both">
<input type="submit" name="sendPhoto" value="Send Photo" />
</div>
</div>
</form>
<?php
}
function displayThanks() {
?>
<h1> Thank You </h1>
<p>thank you for uploading your photo <?php if($_POST["visitorName"]) echo ", " . $_POST["visitorName"] ?> !!! </p>
<p> Here is you photo !!! </p>
<p><img src="photo/<?php echo $_FILES["photo"]["name"] ?>" alt="Photo" /></p>
<?php
}
?>
</body>
</html>