Hi guys. I can't understand why the below image uploaded script won't work.
When I click the submit button, the page just simply reloads and none of the functions seem to be triggered.
<html>
<head>
</head>
<body>
<?php
if( isset( $_POST["upload image"] ) ) {
processForm();
} else {
displayForm();
}
function processForm() {
if( isset( $_FILES["uploadfile"]) && $_FILES["uploadfile"]["error"] == UPLOAD_ERR_OK ) {
if( $_FILES["uploadfile"]["type"] != "image/jpg" ) {
echo "File must be a jpg.";
} elseif( !move_uploaded_file( $_FILES["uploadfile"]["tmp_name"], "images/" . basename( $_FILES["uploadfile"]["name"] ) ) ) {
echo "cannot upload file to server!";
} else {
displayThanks();
}
} else {
switch( $_FILES["uploadfile"]["error"] ) {
case UPLOAD_ERR_NO_FILE:
$message = "no file uploaded";
break;
case UPLOAD_ERR_INI_SIZE:
$message = "file it too big!";
break;
default:
$message = "There is a problem uploading your file. Please";
}
echo $message;
}
}
function displayThanks() {
$firstname = $_POST["firstname"] ? $_POST["firstname"] : "Mr/Mrs";
$lastname = $_POST["lastname"] ? $_POST["lastname"] : "visitor";
?>
<h1>Hi <?php echo $firstname . " " . $lastname ?>!</h1>
<h2>Your file has been uploaded and stored sucessfully!</h2>
<?php
}
function displayForm(){
?>
<form action="practice1.php" method="post" enctype="multipart/form-data">
<br><label for="firstname">First Name:</label><br>
<input type="text" name="firstname" id="firstname" value=""><br><br>
<label for="lastname">Last Name:</label><br>
<input type="text" name="lastname" id="lastname" value=""><br><br>
<label for="uploadfile">Upload File:</label><br>
<input type="file" name="uploadfile" id="uploadfile" value=""><br><br>
<input type="submit" name="upload image" value="upload pic">
</form>
<?php
}
?>
</body>
</html>
Paul.